0
votes

I am passing data from main activity to EditListNameDialogFragment super constructor of DialogFragment in onCreateDialog trowing these exception.

Process: com.example.zar.shoppinglistplusfirebase, PID: 32464 java.lang.NullPointerException at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListDialogFragment.createDialogHepler(EditListDialogFragment.java:67) at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListNameDialogFragment.onCreateDialog(EditListNameDialogFragment.java:47) at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:312) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298) at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5590) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method)

onCreateDialogFragment from EditListNameDialogFragment

public Dialog onCreateDialog(Bundle savedInstanceState) {

    Dialog dialog = super.createDialogHelper(R.string.positive_button_edit_item);
    helpSetDefaultValueEditText(mListName);
    return dialog;
}

super.createDialogHelper(String r) from EditListDialogFragment

 protected Dialog createDialogHepler(int stringResourceforPositiveButton){
    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity(),R.style.CustomTheme_Dialog);
    LayoutInflater inflater=getActivity().getLayoutInflater();

    View rootView=inflater.inflate(mResource,null);
    mEditTextForList=(EditText) rootView.findViewById(R.id.edit_text_list_dialog);
    mEditTextForList.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i== EditorInfo.IME_ACTION_DONE || keyEvent.getAction()==KeyEvent.ACTION_DOWN){
                doListEdit();
                EditListDialogFragment.this.getDialog().cancel();
            }
            return true;
        }
    });
    builder.setView(rootView).setPositiveButton(stringResourceforPositiveButton, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            doListEdit();

            EditListDialogFragment.this.getDialog().cancel();
        }
    })
            .setNegativeButton(R.string.negative_button_cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    EditListDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}
2

2 Answers

0
votes

I think that's because you're not initializing the fragment correctly. You shouldn't use a custom method to initialize the DialogFragment, better to do like this:

public class MyDialogFragment extends DialogFragment {
private textView myTextView;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View rootView = inflater.inflate(R.layout.dialog_list, null);

    // textView = (TextView) ...

    // Set layout for this dialog
    builder.setView(rootView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Send the positive button event back to the host fragment
                }
            });

    // Create the AlertDialog object and return it
    return builder.create();
}
}

In order to get a handle to the calling component to communicate when an action on a button has been performed you should use callbacks. See docs

0
votes

error was with the xml of EditListDialogFragment was not contain the EditText field which called on super.createDialogHelper(String r), i include the EditText field for it and the problem resolve with it.