我有一个带有VIEwPager的Activity和一个包含Fragment的FrameLayout.它看起来像这样:
Activity |— VIEwPager |-FragmentA |— Framelayout |— FragmentB |— FragmentC (here I called fragmentC.setTargetFragment method).
当我旋转设备时.我会收到这个错误:
E/AndroIDRuntime(10354): Caused by: java.lang.IllegalStateException: Fragment no longer exists for key androID:target_state: index 1E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:586)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:885)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1136)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1118)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1922)E/AndroIDRuntime(10354): at androID.support.v4.app.Fragment.performCreate(Fragment.java:1776)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:915)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1136)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1118)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1922)E/AndroIDRuntime(10354): at androID.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)E/AndroIDRuntime(10354): at com.example.androID.animationsdemo.MyActivity.onCreate(MyActivity.java:20)
如果我不调用fragmentC.setTargetFragment方法或我没有为VIEwpager设置适配器,一切正常.
这是我的代码:
活动:
public class MyActivity extends FragmentActivity{ @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.my_activity); VIEwPager mVIEwPager = (VIEwPager) findVIEwByID(R.ID.pager); TabsAdapter tabsAdapter = new TabsAdapter(this); mVIEwPager.setAdapter(tabsAdapter); findVIEwByID(R.ID.button).setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { MyFragmentB fragmentA = new MyFragmentB(); getSupportFragmentManager().beginTransaction().add(R.ID.flContainer,fragmentA,"TAG").addToBackStack("back").commit(); } }); } public static class TabsAdapter extends FragmentPagerAdapter { private final Context mContext; private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); static final class TabInfo { private final Class<?> clss; private final Bundle args; TabInfo(Class<?> _class,Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(FragmentActivity activity) { super(activity.getSupportFragmentManager()); mContext = activity; TabInfo tabInfo = new TabInfo(MyFragmentA.class,null); mTabs.add(tabInfo); } @OverrIDe public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext,info.clss.getname(),info.args); } @OverrIDe public int getCount() { return mTabs.size(); } }}
活动布局:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <button androID:ID="@+ID/button" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="Add Fragment B"/> <FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="300dip" androID:ID="@+ID/flContainer" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="200dp" androID:text="wrtadfa"/> </FrameLayout> <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/pager" androID:layout_below="@+ID/rlContainer" androID:layout_wIDth="match_parent" androID:layout_height="300dip" /></linearLayout>
和FragmentA:
public class MyFragmentA extends Fragment { @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw myFragmentVIEw = inflater.inflate(R.layout.fragment_a,container,false); return myFragmentVIEw; }}
FragmentA布局:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="It's Fragment A" /></linearLayout>
片段B:
public class MyFragmentB extends Fragment implements MyFragmentC.CallBack { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw myFragmentVIEw = inflater.inflate(R.layout.fragment_b,false); myFragmentVIEw.findVIEwByID(R.ID.button).setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { MyFragmentC fragmentA = new MyFragmentC(); //This caused the problem fragmentA.setTargetFragment(MyFragmentB.this,0); getChildFragmentManager().beginTransaction().add(R.ID.flContainer2,"TAG").addToBackStack("back").commit(); } }); return myFragmentVIEw; }}
fragment_b.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="#888888" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="It's Fragment B" /> <TextVIEw androID:ID="@+ID/c_received" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <button androID:ID="@+ID/button" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="Add Fragment C"/> <FrameLayout androID:ID="@+ID/flContainer2" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> </FrameLayout></linearLayout>
FragmentC:
public class MyFragmentC extends Fragment { public static interface CallBack{ //some call back } @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); if(getTargetFragment() != null){ CallBack callBack = (CallBack) getTargetFragment(); } } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw myFragmentVIEw = inflater.inflate(R.layout.fragment_c,false); return myFragmentVIEw; }}
fragment_c.xml:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="#888888" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="It's Fragment C" /></linearLayout>
我知道我可以通过让Activity实现MyFragmentC.CallBack而不是FragmentB来实现它.
但我只是好奇为什么会发生???
如果我没有为vIEwpager设置Adapter或者不在FragmentC中调用“setTargetFragment”.一切都很好.
很抱歉,因为我不知道如何在这里很好地格式化代码,这是一个很长的帖子.任何帮助都可以欣赏.
解决方法 请查看谷歌的讨论主题,建议他们删除setTargetFragment(),而不是使用getParentFragment().更多细节在这里提供: https://code.google.com/p/android/issues/detail?id=54520 总结以上是内存溢出为你收集整理的关键android:target_state不再存在片段全部内容,希望文章能够帮你解决关键android:target_state不再存在片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)