Katedral Pillon
Katedral Pillon

Reputation: 14844

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.

FATAL EXCEPTION: main
 Process: com.company.appname, PID: 8962
 java.lang.NullPointerException
    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)

UPDATE

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 FacebookDialog from inside the onCreateView 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");
            dismiss();
        }
        Log.i(TAG, "leaving requestFaceBookPublishPermission");

    }

Upvotes: 4

Views: 1481

Answers (2)

Sudheesh Mohan
Sudheesh Mohan

Reputation: 2769

I've encountered this issue before. Then I was dismissing the dialog inside oncreateView and I was getting NullpointerException. Then I moved my code to dismiss the dialog to onResume() and the issue was fixed!

Wrong

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false); 

if(something){

} else{
    dismiss(); //Dismissing dialog here gives NullpointerException
    }
}
return view;

Correct

 @Override
 public void onResume() {
    super.onResume();

if(something){
    } else{
        dismiss(); 
        }
    }

Upvotes: 1

Konsol Labapen
Konsol Labapen

Reputation: 2434

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.

Upvotes: 1

Related Questions