简体   繁体   English

DialogFragment神秘的Null指针异常

[英]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 (ie since eclipse is not naming one) 所以也许有人看到了这种神秘的崩溃,我的代码中显然没有根(即因为eclipse没有命名)

UPDATE 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. 所以我有CatDialog(现在崩溃的那个)和FacebookDialog。 So I have had these two DialogFragments working independently. 所以我让这两个DialogFragments独立工作。 But now I am launching FacebookDialog from inside the onCreateView method of the CatDialog. 但现在我从CatDialog的onCreateView方法中启动FacebookDialog 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");

    }

You are calling dismiss() from inside requestFaceBookPublishPermission and that's totally wack! 你正在从requestFaceBookPublishPermission中调用dismiss() ,这完全是怪人! You can't do that if you are calling it from inside onCreateView of another DialogFragment. 如果从另一个DialogFragment的onCreateView内部调用它,则无法执行此操作。 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 . 所以要明白你在做什么:在onCreateView返回视图之前你是在解雇。 You are pre-empting. 你正在抢先一步。

I've encountered this issue before. 我之前遇到过这个问题。 Then I was dismissing the dialog inside oncreateView and I was getting NullpointerException. 然后我解雇了oncreateView里面的对话框,我得到了NullpointerException。 Then I moved my code to dismiss the dialog to onResume() and the issue was fixed! 然后我移动我的代码将对话框关闭到onResume(),问题得到解决!

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(); 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM