我得到了异常
"引起:java.lang.NullPointerException:尝试调用虚拟 方法' void android.os.Bundle.putParcelable(java.lang.String, android.os.Parcelable)'在null对象引用"上 的onSaveInstanceState
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
static Bundle state;
private final String KEY_RECYCLER_STATE = "recycler_state";
static Bundle mBundleRecyclerViewState;
List<Room> roomList = new ArrayList<>();
Room room;
static List<Booking> bookingList = new ArrayList<>();
static RecyclerView recyclerView;
static RecAdapterBooking recAdapterBooking;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Helper.initList(roomList);
Menu menu = navigationView.getMenu();
for (Room r : roomList) {
menu.add(r.getName());
}
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView = (RecyclerView) findViewById(R.id.booking_rec);
recyclerView.setLayoutManager(layoutManager);
final Calendar c = Calendar.getInstance();
final SimpleDateFormat df = new SimpleDateFormat("MMM-dd-yyyy");
final TextView textView = (TextView) findViewById(R.id.time_view);
setTime(c, df, textView);
ImageButton leftButton = (ImageButton) findViewById(R.id.left);
ImageButton rightButton = (ImageButton) findViewById(R.id.right);
leftButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, -1);
textView.setText(df.format(c.getTime()));
}
});
rightButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, +1);
textView.setText(df.format(c.getTime()));
}
});
if (PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getBoolean("pref", true)) {
Intent intent = new Intent(MainActivity.this, SingleRoomActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
this.finish();
}
}
public void setTime(Calendar c, SimpleDateFormat df, TextView textView) {
final String formattedDate = df.format(c.getTime());
textView.setText(formattedDate);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//settings-ზე დაჭერის activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
ArrayList<String> roomNameList = new ArrayList<>();
for (Room r : roomList) {
roomNameList.add(r.getName());
}
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
intent.putExtra("roomlist", roomNameList);
startActivity(intent);
}
if (id == R.id.action_log_out) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
//მენიუდან არჩეული ოთახის განრიგის გამოტანა
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
for (Room r : roomList) {
if (item.getTitle().equals(r.getName())) {
bookingList = r.getBookingList();
recAdapterBooking = new RecAdapterBooking(bookingList, MainActivity.this);
recyclerView.setAdapter(recAdapterBooking);
setTitle(r.getName());
room = r;
break;
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putParcelable(KEY_RECYCLER_STATE, recyclerView.getLayoutManager().onSaveInstanceState());
}
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
// Retrieve list state and list/item positions
if (state != null)
recyclerView.getLayoutManager().onRestoreInstanceState(state.getParcelable(KEY_RECYCLER_STATE));
}
@Override
public void onStop(){
super.onStop();
onSaveInstanceState(state);
}
@Override
public void onRestart(){
super.onRestart();
onRestoreInstanceState(state);
}
}
答案 0 :(得分:0)
检查bundle是否为null,如果是,则初始化它。
@Override
public void onSaveInstanceState(Bundle state) {
if(state==null){
state=new Bundle();
}
super.onSaveInstanceState(state);
state.putParcelable(KEY_RECYCLER_STATE, recyclerView.getLayoutManager().onSaveInstanceState());
}