gpt4 book ai didi

android - 用包含 viewpager 的 fragment 替换 fragment 时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:42 26 4
gpt4 key购买 nike

我创建了一个包含 viewpager 的 HomeFragment,并将其添加到 MainActivity,它运行良好。但是,当我单击转到另一个 fragment (如 BucketFragment),然后单击返回到 homefragment 时,它崩溃了,抛出了空指针异常。

这是主要 Activity

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"/>

当我点击返回homefragment时,它抛出一个运行时异常

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 相同,这意味着堆栈中没有要返回的 fragment ,因此您应该改为放置 add 或添加此行 .addToBackStack(null); .commit 之前。它会让你的操作返回堆栈,然后你可以使用后退按钮返回

关于android - 用包含 viewpager 的 fragment 替换 fragment 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com