我正在尝试保存碎片数据。因此,当我更改手机和互联网的方向时,片段中的数据并没有消失
public class MainActivity extends AppCompatActivity {
public static final String KEY_TITLE = "title";
public static final String KEY_FRAGMENT = "fragment";
private String title= null;
Fragment fragment = new MovieFragment() ;
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_movie:
fragment = new MovieFragment();
title = getString(R.string.movie);
getSupportActionBar().setTitle(title);
break;
case R.id.nav_tv:
fragment = new TvFragment();
title = getString(R.string.tv);
getSupportActionBar().setTitle(title);
break;
}
return setFragment(fragment);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setFragment(new MovieFragment());
title = getString(R.string.movie);
getSupportActionBar().setTitle(title);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();
getSupportActionBar().setTitle(title);
}else {
fragment = getSupportFragmentManager().getFragment(savedInstanceState,KEY_FRAGMENT);
title = savedInstanceState.getString(KEY_TITLE);
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment).commit();
getSupportActionBar().setTitle(title);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_language,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.change_language){
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public boolean setFragment(Fragment fragment){
if (fragment != null){
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameLayout,fragment)
.commit();
return true;
}
return false;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
outState.putString(KEY_TITLE, title);
getSupportFragmentManager().putFragment(outState,KEY_FRAGMENT,fragment);
super.onSaveInstanceState(outState, outPersistentState);
}
当我尝试旋转手机时,应用程序被强制关闭,并且在logcat中显示:
由于:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()' 在androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:396) 在androidx.fragment.app.BackStackRecord.replace(BackStackRecord.java:444) 在androidx.fragment.app.BackStackRecord.replace(BackStackRecord.java:434) 在com.example.submission3.MainActivity.onCreate(MainActivity.java:65) 在android.app.Activity.performCreate(Activity.java:7224) 在android.app.Activity.performCreate(Activity.java:7213) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 在android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4847)
我尝试了很多方法,但是找不到解决方案。 谢谢您的帮助!