DialogFragment:NullPointerException异常(支持库)异常、DialogFragment、NullPointerException

2023-09-07 08:36:09 作者:远去的幸福

我使用的是优秀的 ACRA 库,从我的应用程序收到错误报告。

I'm using the excellent ACRA library to receive error reports from my apps.

我是从关于DialogFragment的NPE客户接收了大量的报道,但是我无法重现它:

I'm receiving a lot of reports from customers concerning an NPE in DialogFragment, but Im unable to reproduce it :

java.lang.NullPointerException
at android.support.v4.app.DialogFragment.onActivityCreated(SourceFile:366)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:892)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1083)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1065)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:1844)
at android.support.v4.app.FragmentActivity.onStart(SourceFile:519)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
at android.app.Activity.performStart(Activity.java:4475)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

在NPE发生在支持库里面(@line 366):

The NPE happens inside the support library (@line 366):

353    @Override
354    public void onActivityCreated(Bundle savedInstanceState) {
           (...)
361        View view = getView();
362        if (view != null) {
363            if (view.getParent() != null) {
364                throw new IllegalStateException("DialogFragment can not be attached             to a container view");
365            }
366            mDialog.setContentView(view);
367        }

林无法重现该问题上我的任何设备(2.2至4.1.1)。由于没有提到我的任何code,它是一个库错误?

Im unable to reproduce the problem on any of my device (from 2.2 to 4.1.1). Since there's no reference to any of my code, is it a library bug?

推荐答案

我曾在一个项目进行调试了同样的问题。

I have had to debug the same issue in a project.

典型对话框片段用作下面

Typically Dialog fragment is used as below

@Override
public Dialog onCreateDialog (Bundle savedInstanceState)
{
  //Create custom dialog
  return dialog;
}

试用更新到以下

Try updating it to the following

@Override
public Dialog onCreateDialog (Bundle savedInstanceState)
{
  //Create custom dialog
  if (dialog == null)
    super.setShowsDialog (false);

  return dialog;
}

这将prevent DialogFragment.onAtivityCreated()的执行方法无效成员变量mDialog上。

This will prevent DialogFragment.onAtivityCreated() from executing methods on the null member variable mDialog.