Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment argument and nullpointer exception

My class should pass an argument to DialogFragment, but my app crashes inside onCreate method (of dialog class) for a NullPointerException. Dialog fragment class portion of code:

public class ConfirmDialog extends DialogFragment {

public ConfirmDialog() {}

 ConfirmDialog newInstance(String f) {
    ConfirmDialog d = new ConfirmDialog();

    Bundle args = new Bundle();
    args.putString("FILE_NAME", f);
    d.setArguments(args);

    return d;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    file = getArguments().getString("FILE_NAME");
}

I have the nullpointer at this line:

file = getArguments().getString("FILE_NAME");

And i don't know why. I paste also the code calls the dialog

private void showConfirmDialog(String file) {
    FragmentManager fm = getSupportFragmentManager();
    ConfirmDialog dialog = new ConfirmDialog();
    Log.i("SHOWFILEACTIVITY", file);
    dialog.newInstance(file);
    dialog.show(fm, "fragment_confirm_dialog");
}

Here the "file" string is not null, i've check it with

Log.i("SHOWFILEACTIVITY", file);
like image 576
giozh Avatar asked Mar 17 '13 17:03

giozh


People also ask

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. A fragment that displays a dialog window, floating on top of its activity's window.

What is DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

You're creating a ConfirmDialog via the constructor, then calling newInstance(), which creates another (proper) ConfirmDialog. However you then discard that proper instance.

To fix this:

Your newInstance() method should be static:

public static ConfirmDialog newInstance(String f) {
    ConfirmDialog d = new ConfirmDialog();

    Bundle args = new Bundle();
    args.putString("FILE_NAME", f);
    d.setArguments(args);

    return d;
}

And showConfirmDialog() should be changed so it uses the newInstance() method properly.

private void showConfirmDialog(String file) {
    FragmentManager fm = getSupportFragmentManager();
    Log.i("SHOWFILEACTIVITY", file);

    ConfirmDialog dialog = ConfirmDialog.newInstance(file);
    dialog.show(fm, "fragment_confirm_dialog");
}
like image 152
A--C Avatar answered Oct 05 '22 00:10

A--C