Katedral Pillon Katedral Pillon - 1 year ago 73
Java Question

DialogFragment mysterious Null Pointer Exception

I cannot seem to make head or tail from the stack trace eclipse is giving me. The code used to work. I am not sure what I changed. But now when I launch the dialog fragment, the app crashes with the following error log.

Process: com.company.appname, PID: 8962
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:366)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
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:5579)
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)

My code is massive so I am not sure which portion to show. So maybe someone has seen this mysterious kind of crash with apparently no root in my code (i.e. since eclipse is not naming one)


Ok. I find the culprit. But I still don’t know how to fix it. So I have CatDialog (the one that’s crashing now) and FacebookDialog. So I have had these two DialogFragments working independently. But now I am launching
from inside the
method of the CatDialog. That is the reason for the crash. The launching is conditional as the following code shows

protected void requestFaceBookPublishPermission() {
Log.i(TAG, "going into requestFaceBookPublishPermission");
Session session = Session.getActiveSession();
if (session == null || !session.isOpened() ||
!session.isPermissionGranted(FacebookDialog.PERMISSION)) {
FacebookDialog fb = new FacebookDialog();
fb.show(getChildFragmentManager(), "fb");
Log.i(TAG, "leaving requestFaceBookPublishPermission");


Answer Source

You are calling dismiss() from inside requestFaceBookPublishPermission and that's totally wack! You can't do that if you are calling it from inside onCreateView of another DialogFragment. So you seed to keep the two separated again. But great question though.

So understand what you are doing: you are dismissing before returning the view inside onCreateView. You are pre-empting.