简体   繁体   English

Android DialogFragment空指针异常

[英]Android DialogFragment null pointer exception

I have a problem with a DialogFragment . 我对DialogFragment有问题。

I've removed all my code and made the DialogFragment as simple as possible and it actually works when called from the MainActivity . 我删除了所有代码,并使DialogFragment尽可能简单,并且在从MainActivity调用时它实际上可以正常工作。 This is the code: 这是代码:

The DatePickerFragment : DatePickerFragment

public class DatePickerFragment extends DialogFragment
{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
       View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false);
       getDialog().setTitle("Simple Dialog");
       return rootView;
   }
}

The call from the MainActivity : 来自MainActivity的电话:

public void showDialog
{
    FragmentManager fm = getFragmentManager();
    DatePickerFragment dialogFragment = new DatePickerFragment ();
    dialogFragment.show(fm, "Sample Fragment");
}

But, if I call showDialog from a class ActivityStarter that has a reference to MainActivity , this is the error I get: 但是,如果我从具有对MainActivity的引用的ActivityStarter类中调用showDialog ,这是我得到的错误:

java.lang.NullPointerException
        at android.widget.AbsListView.obtainView(AbsListView.java:2353)
        at android.widget.ListView.makeAndAddView(ListView.java:1812)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillSpecific(ListView.java:1359)
        at android.widget.ListView.layoutChildren(ListView.java:1623)
        at android.widget.AbsListView.onLayout(AbsListView.java:2149)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        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:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

I'm really stuck and cannot find a solution. 我真的很困,无法找到解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。

OK, found the reason. 好,找到原因了。 Thanks to all but the problem is not caused by the DialogFragment that has nothing to do with this. 多亏了所有,但是问题不是由与此无关的DialogFragment引起的。

The crash was caused by the method call just before the DialogFragment.show() and that was confusing me. 崩溃是由DialogFragment.show()之前的方法调用引起的,这让我感到困惑。

I explain even though this will never help anyone as it's a problem just of my code: 我解释了一下,尽管这对任何人都没有帮助,因为这只是我的代码的问题:

I'm opening the DatePickerFragment exactly after the user has inserted his name. DatePickerFragment在用户插入他的名字之后打开DatePickerFragment Before, the name was inside a child of an ExpandableListView 's group so I was calling expandGroup() to show it. 以前,该名称位于ExpandableListView组的子级中,因此我要调用expandGroup()进行显示。 Actually the name is not changable anymore so it's not in a child but in a locked group of the ExpandableListView . 实际上,该名称不再可更改,因此它不在子级中,而是在ExpandableListView的锁定组中。 I just forgot to remove the call to expandGroup() that, with no children, was causing the crash (that's why the ListView problem). 我只是忘了删除没有引起孩子崩溃的expandGroup()调用(这就是ListView问题的原因)。

Thanks again, posting here helped me anyway. 再次感谢您,在这里发布仍然对我有帮助。

In MainActivity: 在MainActivity中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityStarter activityStarter = new ActivityStarter(this);
}

public onSomeEvent() {
    activityStarter.showDialog();
}

In ActivityStarter: 在ActivityStarter中:

Context context;

ActivityStarter(Context context) {
    this.context = context;
}

public void showDialog() {
    FragmentManager fm = context.getFragmentManager();
    DatePickerFragment dialogFragment = new DatePickerFragment ();
    dialogFragment.show(fm, "Sample Fragment");
}

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

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