我创建了一个包含viewpager的HomeFragment,并将其添加到MainActivity中,它运行正常。但当我点击进入另一个片段如BucketFragment然后点击返回homefragment时,它崩溃了,抛出了一个nullpointer异常。
这是MainActivity
#include <iostream>
using namespace std;
int main() {
int P=1000;
int i=0.0275;
int FV;
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
这是HomeFragment
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;
}
}
和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
当我点击返回homefragment时,它会抛出一个运行时异常
<?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"/>
答案 0 :(得分:3)
问题在于这段代码
if (currentFragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.fragment_container, currentFragment)
.commit();
setTitle(item.getTitle());
}
替换方法用作删除+添加,这意味着您没有要返回的堆栈中的片段,因此您应该改为添加add
或在.addToBackStack(null);
之前添加此行.commit
。它会将您的操作置于Backstack,然后您可以使用后退按钮
答案 1 :(得分:0)
如果您将addToBackStack(null)
用于supportFragmentManager
,则每个片段在开始时都会重新加载,因此fragment
不会保留在堆栈中。
我已将childFragmentManager
中的supportFragmentManager
更改为android.support.v4.app.Fragment
并开始工作。