如何在ViewPager中正确销毁视图?我创建了包含ListFragment的应用程序,该ListFragment填充了列表项。在列表项单击上,将创建新片段并将其添加到FragmentPagerAdapter。一切正常,直到我尝试从ViewPager中删除视图。调用方法mViewPager.removeViewAt(position);在给定位置留下空白页。如果我在删除视图后滚动到另一个页面并滚动回那个空页面应用程序崩溃。
@Override
public void onListItemClicked(int position) {
Log.i("MainActivity", "Listitem clicked! position: " + position);
FragmentContent fragment = new FragmentContent();
Bundle bundle = new Bundle();
bundle.putString("url", Directory.urls[position]);
fragment.setArguments(bundle);
Tab tab = mActionBar.newTab().setText(Directory.boards[position]).setTag(fragment).setTabListener(this);
mActionBar.addTab(tab, true);
// Call this method to update ActionBar
invalidateOptionsMenu();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.i("MainActivity", "SectionsPagerAdapter.getItem() position: " + position);
// ActionBar tag contains fragment
Fragment frag = (Fragment)mActionBar.getTabAt(position).getTag();
return frag;
}
@Override
public int getCount() {
return mActionBar.getTabCount();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){
// Remove page and tab at given position
mViewPager.removeViewAt(position);
// ActionBar tag contains fragment
Fragment frag = (Fragment)mActionBar.getTabAt(position).getTag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(frag).commit();
mActionBar.removeTab(mActionBar.getTabAt(position));
// Update options menu
invalidateOptionsMenu();
notifyDataSetChanged();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_close:
Log.i("MainActivity", "onOptionsItemSelected() close thread button pressed!");
mSectionsPagerAdapter.destroyItem(null, mActionBar.getSelectedTab().getPosition(), null);
mSectionsPagerAdapter.notifyDataSetChanged();
break;
}
return true;
}
日志:
2-24 20:41:32.578: I/FragmentList(10185): Item clicked: 4
02-24 20:41:32.593: I/MainActivity(10185): SectionsPagerAdapter.getItem() position: 1
02-24 20:41:32.593: I/FragmentContent(10185): FragmentContent onAttach
02-24 20:41:32.601: I/FragmentContent(10185): FragmentContent onResume()
// This is the point where i try to destroy page
02-24 20:41:47.273: I/MainActivity(10185): onOptionsItemSelected() close thread button pressed!
02-24 20:41:47.289: I/FragmentContent(10185): FragmentContent onPause()
02-24 20:41:47.289: I/FragmentContent(10185): FragmentContent onDestroy
// This is the point i scroll back to that empty page
02-24 20:42:09.546: W/dalvikvm(10185): threadid=1: thread exiting with uncaught exception (group=0x40ab21f8)
02-24 20:42:09.546: E/AndroidRuntime(10185): FATAL EXCEPTION: main
02-24 20:42:09.546: E/AndroidRuntime(10185): java.lang.NullPointerException
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.view.ViewPager.populate(ViewPager.java:1010)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.MoTo.ylilauta.MainActivity.onTabSelected(MainActivity.java:198)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:512)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:441)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.MoTo.ylilauta.MainActivity.onListItemClicked(MainActivity.java:147)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.MoTo.ylilauta.FragmentList.onListItemClick(FragmentList.java:48)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.widget.AbsListView.performItemClick(AbsListView.java:1082)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2625)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.widget.AbsListView$1.run(AbsListView.java:3296)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.os.Handler.handleCallback(Handler.java:605)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.os.Looper.loop(Looper.java:137)
02-24 20:42:09.546: E/AndroidRuntime(10185): at android.app.ActivityThread.main(ActivityThread.java:4697)
02-24 20:42:09.546: E/AndroidRuntime(10185): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 20:42:09.546: E/AndroidRuntime(10185): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-24 20:42:09.546: E/AndroidRuntime(10185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
02-24 20:42:09.546: E/AndroidRuntime(10185): at dalvik.system.NativeStart.main(Native Method)
02-24 20:42:11.632: I/Process(10185): Sending signal. PID: 10185 SIG: 9