Eliezer
Eliezer

Reputation: 7357

Android Support v4 DialogFragment Crashing Application

I'm getting this exception from my error logs that my app sends when it crashes:

java.lang.RuntimeException: Unable to start activity ComponentInfo{<my activity>}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553)
    at android.app.ActivityThread.access$700(ActivityThread.java:140)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4898)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:368)
    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
    at <my FragmentActivity wrapper>.onStart(BaseActivity.java:16)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
    at android.app.Activity.performStart(Activity.java:5216)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
    ... 12 more

I have been unable to reproduce it, and the logs leading up to this crash don't seem to indicate that any DialogFragment was shown. This is the line in the support library that is causing the crash

Upvotes: 3

Views: 2762

Answers (2)

Steve B
Steve B

Reputation: 1246

mDialog is null, meaning that your dialog wasn't properly created. I had the same situation where this would occur, but in very limited circumstances (nothing to do with screen rotation, but it was something I could reproduce 100% of the time), and it had to do with onCreateView() not properly creating the dialog, leading to the null mDialog. onCreateView() can be used to create a dialog -- the Basic Dialog section of http://developer.android.com/reference/android/app/DialogFragment.html even shows this -- but I proved that it is NOT always reliable. However, in my crash scenario, I found that using onCreateDialog() instead of onCreateView() would always work, even though I was using the same layout and data that my onCreateView() was using. So I changed my code to use onCreateDialog() when using the DialogFragment as a dialog and that solved the issue. So that is one thing you may want to check.

If you want a brute force method to stop the crash (though the root cause of why mDialog is null would remain), you can use this code -- which I used successfully until I found the real issue above:

@Override
public void onActivityCreated(Bundle arg0) {
    if (getDialog() == null ) {  // Returns mDialog
        // Tells DialogFragment to not use the fragment as a dialog, and so won't try to use mDialog
        setShowsDialog( false ); 
    } 
    super.onActivityCreated(arg0);  // Will now complete and not crash

}

Upvotes: 2

Phuong
Phuong

Reputation: 1383

The answer here: DialogFragment : NullPointerException (support library)

Add this code:

@Override
public Dialog onCreateDialog (Bundle savedInstanceState)

  //Create custom dialog
  if (dialog == null)
    super.setShowsDialog (false);

  return dialog;
}

Upvotes: 1

Related Questions