嵌套片段找不到ID为

时间:2017-12-20 11:11:47

标签: android android-fragments tabs android-nested-fragment

我正在处理具有标签布局的嵌套片段的简单应用程序。父活动是LandingActivity。在LandingActivity里面我称之为LandingFragment。在LandingFragment内部,我有以下功能。

Tasks TAB Fragments flow is TasksMainFragment->TasksFragment->DetailFragment.

Pending TAB Fragments flow is PendingMainFragment->PendingFragment->PendingDetailFragment

以下代码表示从TasksMainFragment调用TasksFragment。

TasksFragment fragment=new TasksFragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.id_frame, fragment, "tasks");
        fragmentTransaction.addToBackStack("tasks");
        fragmentTransaction.commit();

下面的代码表示从TasksFragment调用DetailFragment。基本上,TasksFragment包含具有一些任务的recyclerview。当用户点击任务时,它将打开任务的DetailFragment。

Fragment fragment = new DetailFragment();

        FragmentManager manager = ((FragmentActivity) mContext).getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        Bundle bundle = new Bundle();
        ItemModel item = list.get(position);
        bundle.putSerializable("item", item);
        bundle.putBoolean("having", havingStartedService());
        fragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.id_frame, fragment, "details");
        fragmentTransaction.addToBackStack("details");
        fragmentTransaction.commit();

当用户切换到挂起标签时,相同的行为或相同的功能将起作用。但问题是,当我在一段时间后打开应用程序或每天早上第一次打开应用程序时,我正面临这个问题。

当我更改屏幕方向时,我也面临这个问题。

我在下面发布了logcat以便更好地理解。

12-20 16:33:01.658 21590-21590/com.naushad.kenostaff E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.naushad.kenostaff, PID: 21590
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.naushad.kenostaff/com.naushad.kenostaff.landing.LandingActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070059 (com.naushad.kenostaff:id/id_frame) for fragment TasksFragment{b980f0d #7 id=0x7f070059 tasks}
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                                           at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4556)
                                                                           at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                                        Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f070059 (com.naushad.kenostaff:id/id_frame) for fragment TasksFragment{b980f0d #7 id=0x7f070059 tasks}
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                           at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                           at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                           at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                           at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                           at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
                                                                           at com.stfalcon.androidmvvmhelper.mvvm.activities.BindingActivity.onStart(BindingActivity.java:50)
                                                                           at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
                                                                           at android.app.Activity.performStart(Activity.java:6701)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) 
                                                                           at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4556) 
                                                                           at android.app.ActivityThread.-wrap19(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6186) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

请在这个问题上给我一些帮助。我过去一周面对这个问题,我尝试了很多方法,但没有任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:2)

使用 getChildFragmentManager()而不是 getActivity.getSupportFragmentManager() .i解决了两次崩溃。

对于打开TasksFragment,我使用下面的代码。

foreach ($content as $dates => $row) { $date = $dates;

为了打开DetailFragment,我使用下面的代码。

FragmentManager fragmentManager = getChildFragmentManager(); 在任务选项卡中。在挂起的选项卡中,我也按照相同的方式解决崩溃问题。

现在我正面临其他问题。我正在检查在TasksMainFragment和PendingMainFragment中用户可以看到哪个片段。因为我在两个主要片段中打开了两个片段。所以取决于我想要更新可见片段的UI。

通过使用下面的代码,我正在检查哪个片段对用户可见。

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

但是当我在TasksFragment中时,上面的代码无效。当我在DetailFragment上面的代码工作正常。请帮我解决这个问题,或者让我知道我的方式是否有任何问题。