编程语言
首页 > 编程语言> > Android支持v4 DialogFragment崩溃应用程序

Android支持v4 DialogFragment崩溃应用程序

作者:互联网

我从应用崩溃时发送的错误日志中获取了此异常:

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

我一直无法复制它,导致崩溃的日志似乎并不表明已显示任何DialogFragment. This is the line in the support library that is causing the crash

解决方法:

mDialog为null,表示未正确创建对话框.我曾经发生过同样的情况,但是在非常有限的情况下(与屏幕旋转无关,但这是我可以100%的时间再现的东西),并且这与onCreateView()无法正确创建对话框,导致空的mDialog. onCreateView()可用于创建对话框-http://developer.android.com/reference/android/app/DialogFragment.html的“基本对话框”部分甚至显示了此内容-但我证明它并不总是可靠的.但是,在崩溃的情况下,我发现即使使用与onCreateView()相同的布局和数据,也可以始终使用onCreateDialog()而不是onCreateView()来工作.因此,当将DialogFragment用作对话框时,我将代码更改为使用onCreateDialog(),从而解决了该问题.因此,这可能是您要检查的一件事.

如果您希望使用蛮力方法来阻止崩溃(尽管仍然会保留mDialog为null的根本原因),则可以使用此代码-在发现上述实际问题之前,我一直使用该代码:

@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

}

标签:android-fragments,android-support-library,android-dialogfragment,android
来源: https://codeday.me/bug/20191122/2059994.html