android-将片段替换为包含viewpager的片段时崩溃
作者:互联网
我创建了一个包含viewpager的HomeFragment,并将其添加到MainActivity中,效果很好.但是,当我单击以转到另一个片段(如BucketFragment),然后单击以返回到家庭片段时,它崩溃了,并抛出了nullpointer异常.
这是MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mDrawer;
private HomeFragment mHomeFragment;
private LikeFragment mLikeFragment;
private BucketFragment mBucketFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// add the fragment content
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = HomeFragment.newInstance();
mHomeFragment = (HomeFragment) fragment;
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment currentFragment = null;
Intent intent = null;
switch (item.getItemId()) {
case R.id.nav_home:
if (mHomeFragment == null) {
mHomeFragment = HomeFragment.newInstance();
}
currentFragment = mHomeFragment;
break;
case R.id.nav_like:
if (mLikeFragment == null) {
mLikeFragment = LikeFragment.newInstance();
}
currentFragment = mLikeFragment;
break;
case R.id.nav_bucket:
if (mBucketFragment == null) {
mBucketFragment = BucketFragment.newInstance();
}
currentFragment = mBucketFragment;
break;
case R.id.nav_help:
intent = HelpActivity.newIntent(this);
break;
case R.id.nav_feedback:
intent = FeedBackActivity.newIntent(this);
break;
default:break;
}
if (currentFragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.fragment_container, currentFragment)
.commit();
setTitle(item.getTitle());
} else {
startActivity(intent);
}
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是HomeFragment
public class HomeFragment extends Fragment {
private static final int NUM_CATOGARY = 2;
private ViewPager mViewPager;
public static HomeFragment newInstance() {
return new HomeFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
mViewPager = (ViewPager) v.findViewById(R.id.view_pager);
TabLayout tab = (TabLayout) v.findViewById(R.id.tab);
tab.setupWithViewPager(mViewPager);
FragmentManager fm = getChildFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return PopularFragment.newInstance();
case 1:
return RecentFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
return NUM_CATOGARY;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getText(R.string.tab_popular);
case 1:
return getResources().getText(R.string.tab_recent);
}
return null;
}
});
return v;
}
}
和fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
当我单击以返回到家庭片段时,它将引发运行时异常
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:902)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1455)
at android.view.View.dispatchRestoreInstanceState(View.java:14956)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3253)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3259)
at android.view.View.restoreHierarchyState(View.java:14934)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:396)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1445)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5770)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
解决方法:
问题出在这段代码中
if (currentFragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.fragment_container, currentFragment)
.commit();
setTitle(item.getTitle());
}
replace方法的作用与remove add相同,这意味着您在堆栈中没有要返回的片段,因此您应该放入add代替或添加此行.addToBackStack(null); .commit之前.它会将您的操作放回堆栈,然后您可以使用“后退”按钮返回
标签:android-fragments,android-viewpager,android 来源: https://codeday.me/bug/20191110/2013263.html