我读到了关于这个问题的最后一个问题,但没有找到有帮助的解决方案,我只是不明白问题是什么。 代码已经工作,但似乎有些后续工作已经破坏了它,然后我删除了我所做的每一个更改,但我仍然得到这个错误。 我希望有人能更多地了解这个问题并帮助我。
06-22 22:59:01.966: E/AndroidRuntime(4701): java.lang.IllegalStateException: Can't change tag of fragment CardFragment{426ce8a0 #1 id=0x7f0e001f android:switcher:2131623967:1}: was android:switcher:2131623967:1 now android:switcher:2131623967:2
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:399)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.BackStackRecord.add(BackStackRecord.java:390)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v13.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer.doFrame(Choreographer.java:543)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Handler.handleCallback(Handler.java:733)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.os.Looper.loop(Looper.java:136)
06-22 22:59:01.966: E/AndroidRuntime(4701): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-22 22:59:01.966: E/AndroidRuntime(4701): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 22:59:01.966: E/AndroidRuntime(4701): at java.lang.reflect.Method.invoke(Method.java:515)
06-22 22:59:01.966: E/AndroidRuntime(4701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-22 22:59:01.966: E/AndroidRuntime(4701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-22 22:59:01.966: E/AndroidRuntime(4701): at dalvik.system.NativeStart.main(Native Method)
这是MainActivity中的代码,我使用MyPagerAdapter作为片段
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.app_name);
getActionBar().setIcon(R.drawable.ic_action_logo);
setContentView(R.layout.main);
getActionBar().setDisplayUseLogoEnabled(true);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager1);
mColors = new int[]{
R.color.myGreen,
R.color.blue,
R.color.orange,
R.color.red,
R.color.green,
};
adapter = new MyPagerAdapter(getFragmentManager());
pager.setAdapter(adapter);
int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
//use the database
openDB();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
@Override
public void onPause(){
super.onPause();
closeDB();
}
private void closeDB() {
myDB.close();
}
private void openDB() {
myDB = new DBAdapter(this);
myDB.open();
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
TITLES = getResources().getStringArray(R.array.tab_titles);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0: cardF0 = new CardFragment(TITLES[0], mColors[0]); return cardF0;
case 1: cardF1 = new CardFragment(TITLES[1], mColors[0]); return cardF1;
case 2: return cardF1;
}
return cardF0;
}
}
编辑:忘了发布变种
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private static CardFragment cardF0;
private static CardFragment cardF1;
答案 0 :(得分:11)
I think this link can explain whats happening here.
这可能是因为您正在添加相同的片段实例三 时间到列表。您应该为每个页面创建一个新实例。
由于数组中有3个项目,但返回2个片段,其中1个重新使用的片段将返回&再次共享相同的标签可能会导致此异常。所以只需创建新实例,让适配器为您处理内存效率。
另外,不要让你的片段静态,否则它们的上下文(容器活动)永远不会在内存中释放,从而导致巨大的活动内存泄漏。