<small id='rysJP'></small><noframes id='rysJP'>

      • <bdo id='rysJP'></bdo><ul id='rysJP'></ul>
    1. <tfoot id='rysJP'></tfoot>
        <i id='rysJP'><tr id='rysJP'><dt id='rysJP'><q id='rysJP'><span id='rysJP'><b id='rysJP'><form id='rysJP'><ins id='rysJP'></ins><ul id='rysJP'></ul><sub id='rysJP'></sub></form><legend id='rysJP'></legend><bdo id='rysJP'><pre id='rysJP'><center id='rysJP'></center></pre></bdo></b><th id='rysJP'></th></span></q></dt></tr></i><div id='rysJP'><tfoot id='rysJP'></tfoot><dl id='rysJP'><fieldset id='rysJP'></fieldset></dl></div>
      1. <legend id='rysJP'><style id='rysJP'><dir id='rysJP'><q id='rysJP'></q></dir></style></legend>
      2. 收到错误“Java.lang.IllegalStateException Activity 已被破坏";在 V

        时间:2023-10-04
          <tbody id='tnyLg'></tbody>

            • <tfoot id='tnyLg'></tfoot>
              • <bdo id='tnyLg'></bdo><ul id='tnyLg'></ul>

              • <small id='tnyLg'></small><noframes id='tnyLg'>

                <legend id='tnyLg'><style id='tnyLg'><dir id='tnyLg'><q id='tnyLg'></q></dir></style></legend>

              • <i id='tnyLg'><tr id='tnyLg'><dt id='tnyLg'><q id='tnyLg'><span id='tnyLg'><b id='tnyLg'><form id='tnyLg'><ins id='tnyLg'></ins><ul id='tnyLg'></ul><sub id='tnyLg'></sub></form><legend id='tnyLg'></legend><bdo id='tnyLg'><pre id='tnyLg'><center id='tnyLg'></center></pre></bdo></b><th id='tnyLg'></th></span></q></dt></tr></i><div id='tnyLg'><tfoot id='tnyLg'></tfoot><dl id='tnyLg'><fieldset id='tnyLg'></fieldset></dl></div>
                  本文介绍了收到错误“Java.lang.IllegalStateException Activity 已被破坏";在 ViewPager 中使用标签时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个应用程序,它包含在选项卡模式下使用 ActionBarSherlock.我有 5 个选项卡,每个选项卡的内容都使用片段处理.不过,对于 tab2,我有一个片段,其中的 xml 文件包含一个 ViewPager 元素,该元素又具有一些片段页面.当我最初启动应用程序时,我可以在选项卡之间切换没有问题,但是当我第二次按下 tab2 时,我得到了上面提到的错误.主要活动如下:

                  I have an application that consists of using ActionBarSherlock in tab mode.I have 5 tabs and the content of each tab is handled using fragments. For tab2 though, I have a fragment the xml file of which holds a ViewPager element which in turn has some fragment pages. When I initially start the application the application, I am able to switch between tabs no problem but when I press on tab2 for the second time I get the error mentioned above. The main activity is as follows:

                  public class MainActivity extends SherlockFragmentActivity
                  {
                      @Override
                      protected void onCreate(Bundle savedInstanceState) 
                      {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);
                  
                          ActionBar actionBar = getSupportActionBar();
                  
                          ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
                          ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3");
                          ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
                          ActionBar.Tab tab4 = actionBar.newTab().setText("Tab4");
                          ActionBar.Tab tab5 = actionBar.newTab().setText("Tab5");
                  
                          Fragment fragment1 = new Tab1();
                          Fragment fragment3 = new Tab3();
                          Fragment fragment2 = new Tab2();
                          Fragment fragment5 = new Tab5();
                          Fragment fragment4 = new Tab4();
                  
                          tab1.setTabListener(new MyTabListener(fragment1));
                          tab3.setTabListener(new MyTabListener(fragment3));
                          tab2.setTabListener(new MyTabListener(fragment2));
                          tab5.setTabListener(new MyTabListener(fragment5));
                          tab4.setTabListener(new MyTabListener(fragment4));
                  
                          actionBar.addTab(tab1);
                          actionBar.addTab(tab2);
                          actionBar.addTab(tab3);
                          actionBar.addTab(tab4);
                          actionBar.addTab(tab5); 
                  
                          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                      }
                  
                      class MyTabListener implements ActionBar.TabListener
                      {
                          Fragment fragment;
                  
                          public MyTabListener(Fragment fragment)
                          {
                              this.fragment = fragment;
                          }
                  
                          @Override
                          public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,FragmentTransaction ft) 
                          {
                              ft.replace(R.id.fragment_container,fragment);
                          }
                  
                          @Override
                          public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,FragmentTransaction ft) 
                          {
                  
                          }
                  
                          @Override
                          public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,FragmentTransaction ft) 
                          {
                  
                          }
                      }
                  }
                  

                  没有ViewPager的片段类如下:

                  The fragment class without the ViewPager is as follows:

                  public class Tab1 extends Fragment 
                  {
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
                      {
                          return inflater.inflate(R.layout.activity_tab1, container, false);
                      }
                  }
                  

                  带有ViewPager的片段类如下:

                  The fragment class with the ViewPager is as follows:

                  public class Tab2 extends Fragment 
                  {
                      ViewPager mViewPager;
                      private MyFragmentPagerAdapter mMyFragmentPagerAdapter;  
                      private static int NUMBER_OF_PAGES = 5;
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
                      {
                          View view =  inflater.inflate(R.layout.activity_tab2, container, false); 
                          return view;
                      }
                  
                      @Override
                      public void onViewCreated(View view,Bundle savedInstanceState)
                      {
                          super.onViewCreated(view, savedInstanceState);
                          mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
                          mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());  
                          mViewPager.setAdapter(mMyFragmentPagerAdapter);  
                      }
                  
                      private static class MyFragmentPagerAdapter extends FragmentPagerAdapter 
                      {    
                          public MyFragmentPagerAdapter(FragmentManager fm) 
                          {  
                               super(fm);  
                          }  
                  
                          @Override  
                          public Fragment getItem(int index) 
                          {  
                               return PageFragment.newInstance("My Message " + index);
                          }  
                  
                          @Override  
                          public int getCount() 
                          {  
                               return NUMBER_OF_PAGES;  
                          }  
                     }
                  }
                  

                  从我在不同地方读到的内容(如果我错了,请纠正我),发生这种情况是因为第二遍的片段管理器试图重用不再存在的活动中的片段,从而给出错误.但我不确定为什么会发生这种情况,因为我没有使用片段活动.根据 logcat,错误出现在 Tab2 类中,onViewCreated 方法位于 mViewPager.setAdapter(mMyFragmentPagerAdapter) 行上.非常感谢任何帮助...谢谢.

                  From what I've read in different places (and please correct me if I'm wrong), this happens because the fragment manager on the second pass tries to reuse the fragments from the activity which doesn't exist anymore thus giving the error.But I'm not sure why this happens over here since I'm not using fragment activity. According to logcat the error is in the Tab2 class, onViewCreated method on the line that says mViewPager.setAdapter(mMyFragmentPagerAdapter). Any help is greatly appreciated...Thanks.

                  03-04 12:01:05.468: E/AndroidRuntime(2474): FATAL EXCEPTION: main
                  03-04 12:01:05.468: E/AndroidRuntime(2474): java.lang.IllegalStateException: Activity has been destroyed
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:433)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at com.example.tabs.Tab2.onViewCreated(Tab2.java:31)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.os.Handler.handleCallback(Handler.java:587)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.os.Handler.dispatchMessage(Handler.java:92)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.os.Looper.loop(Looper.java:123)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at android.app.ActivityThread.main(ActivityThread.java:3687)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at java.lang.reflect.Method.invokeNative(Native Method)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at java.lang.reflect.Method.invoke(Method.java:507)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                  03-04 12:01:05.468: E/AndroidRuntime(2474):     at dalvik.system.NativeStart.main(Native Method)
                  

                  推荐答案

                  这似乎是新添加的对嵌套片段的支持中的一个错误.基本上,当子 FragmentManager 从活动中分离出来时,它的内部状态会中断.为我修复它的短期解决方法是将以下内容添加到您调用 getChildFragmentManager() 的每个 FragmentonDetach():

                  This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:

                  @Override
                  public void onDetach() {
                      super.onDetach();
                  
                      try {
                          Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
                          childFragmentManager.setAccessible(true);
                          childFragmentManager.set(this, null);
                  
                      } catch (NoSuchFieldException e) {
                          throw new RuntimeException(e);
                      } catch (IllegalAccessException e) {
                          throw new RuntimeException(e);
                      }
                  }
                  

                  这篇关于收到错误“Java.lang.IllegalStateException Activity 已被破坏";在 ViewPager 中使用标签时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android:垂直 ViewPager 下一篇:ViewPager.setOffscreenPageLimit(0) 无法按预期工作

                  相关文章

                • <legend id='o0Ole'><style id='o0Ole'><dir id='o0Ole'><q id='o0Ole'></q></dir></style></legend>
                  <i id='o0Ole'><tr id='o0Ole'><dt id='o0Ole'><q id='o0Ole'><span id='o0Ole'><b id='o0Ole'><form id='o0Ole'><ins id='o0Ole'></ins><ul id='o0Ole'></ul><sub id='o0Ole'></sub></form><legend id='o0Ole'></legend><bdo id='o0Ole'><pre id='o0Ole'><center id='o0Ole'></center></pre></bdo></b><th id='o0Ole'></th></span></q></dt></tr></i><div id='o0Ole'><tfoot id='o0Ole'></tfoot><dl id='o0Ole'><fieldset id='o0Ole'></fieldset></dl></div>

                    <small id='o0Ole'></small><noframes id='o0Ole'>

                    • <bdo id='o0Ole'></bdo><ul id='o0Ole'></ul>

                    1. <tfoot id='o0Ole'></tfoot>