Search code examples
androidandroid-fragmentsandroid-listfragmentandroid-dialogfragment

getTargetFragment() returns null -- works in one place but not another


I am having an odd problem. I am implementing the same code in two places (both in ListFragments), but one works and the other throws a NullPointerException.

Here's my code (all part of a subclass of ListFragment):

public static class DeleteDialogFragment extends DialogFragment {

    static DeleteDialogFragment newInstance(int arg) {
        return new DeleteDialogFragment();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
            .setTitle(R.string.confirm_delete)
            .setMessage(R.string.teacher_confirm_delete_message)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ((TeachersFragment)getTargetFragment()).deleteTeacher();
                }
            })
            .setNegativeButton(R.string.no, null)
            .create();
    }
}

public void deleteTeacher() {
    DbUtils.deleteTeacher(context, selectedItem);
}

I have exactly the same thing in another ListFragment (except I'm casting it to an AssignmentListFragment instead of a TeachersFragment), and that one works flawlessly. In both cases the DeleteDialogFragment class is nested directly under the ListFragment.

The error message I'm getting is as follows:

E/AndroidRuntime(15282): FATAL EXCEPTION: main
E/AndroidRuntime(15282): java.lang.NullPointerException
E/AndroidRuntime(15282):    at com.acedit.assignamo.setup.TeachersFragment.access$6(TeachersFragment.java:145)
E/AndroidRuntime(15282):    at com.acedit.assignamo.setup.TeachersFragment$DeleteDialogFragment$1.onClick(TeachersFragment.java:137)
E/AndroidRuntime(15282):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
E/AndroidRuntime(15282):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15282):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15282):    at android.app.ActivityThread.main(ActivityThread.java:4575)
E/AndroidRuntime(15282):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15282):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15282):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(15282):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(15282):    at dalvik.system.NativeStart.main(Native Method)

Given that getTargetFragment() returns null (I checked in debug mode by assigning the return value to a temporary variable and it is null), it seems odd that it makes it to the deleteTeacher() method before crashing (public void deleteTeacher() { is line 145).

Any ideas what the heck is wrong? If you need more code, just let me know.

EDIT: Here is the code where I call setTargetFragment (in my other instance of this code I was able to use this instead of getTargetFragment, so I think that is where the problem lies.

private class CustomOnClickListener implements OnClickListener {
    public void onClick(View v) {
        Toast.makeText(context, "Button pressed", Toast.LENGTH_SHORT).show();
        if (v == viewHolder.editButton)
            startActivity(new Intent(context, getEditClass()).putExtra(Values.KEY_ROWID, (Long)v.getTag()));
        else { // Delete it
            selectedItem = (Long) v.getTag();
            DeleteDialogFragment frag = new DeleteDialogFragment();
            frag.setTargetFragment(getTargetFragment(), 0);
            frag.show(getFragmentManager(), "confirmDelete");
        }
    }

}

Solution

  • The problem is you don't have a reference to the TeacherFragment since getTargetFragment() is null, but you can fetch the Fragment other ways try using the FragmentManager's getFragmentById() or getFragmentByTag() methods.