gpt4 book ai didi

android - ChildFragmentManager.getFragment() 和 NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:54 28 4
gpt4 key购买 nike

我有一个托 pipe fragment 的父 fragment 。我对我的应用程序进行了压力测试,通过在开发人员选项中选中“不保留 Activity ”,然后按下主页按钮来强制终止它。

可以看到父 fragment 的onSaveInstanceState被调用了,函数的代码是:

if (mDetailFragment!=null){
getChildFragmentManager().putFragment(outState,FRAGMENT_ROUTE,mDetailFragment);
}

然后,当我恢复应用程序时,看到调用父 fragment 的 onCreate 时使用了 NON-null 和 NON-empty savedInstanceState,我可以看到包含索引 FRAGMENT_ROUTE 的包。但是,当我调用 getFragment 时,它总是崩溃。代码如下:

if (savedInstanceState == null) {
mDetailFragment = new RouteFragment();
fragmentManager.beginTransaction().replace(R.id.v4_fragment_route, mDetailFragment).commit();
} else {
//first try to find the fragment in a normal way
mDetailFragment = (RouteFragment) fragmentManager.findFragmentById(R.id.v4_fragment_route);
//if cannot, then use getFragment
if (mDetailFragment== null){
mDetailFragment = (RouteFragment) fragmentManager.getFragment(savedInstanceState,FRAGMENT_ROUTE);
}
}

崩溃日志如下,看起来索引列表是空的。我错过了什么?谢谢

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:583)
at com.myapp.android.appname.fragment.HomeFragment.onCreate(HomeFragment.java:170)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1764)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1109)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1913)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:267)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.buzzhives.android.tripplanner.ProgressEnabledActivity.onCreate(ProgressEnabledActivity.java:25)
at com.buzzhives.android.tripplanner.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5934)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5223)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

fragment (无 fragment )已经创建。在任何 fragment 存在或崩溃之前,您不能调用 getFragment。它可能不应该(应该可能返回 null),但它是一个错误。

关于android - ChildFragmentManager.getFragment() 和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29325924/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com