NullPointerException trying to retrieve savedInstanceState with Custom Class Array
I am having a problem trying to get my program to keep information in my variables when the screen rotates. Right now it resets everything when it rotates. My information is stored in an array of a custom class called Players. I have tried using putParcelableArray and getParcelableArray and have Implemented Parcelable in the custom class and the required code for it. When I run the code it pops up the force close menu and the debugger gives NullPointerException on my code for checking if savedInstanceState is empty.
Here is part of the main code where the problem occurs.
package nicholsoft.LevelCounter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class LevelCounter extends Activity implements OnClickListener {
public Player [] Players;
TextView p1text;
TextView p2text;
TextView p3text;
TextView p4text;
TextView p5text;
TextView p6text;
TextView p1Level;
ImageView p1Epic;
TextView p2Level;
ImageView p2Epic;
TextView p3Level;
ImageView p3Epic;
TextView p4Level;
ImageView p4Epic;
TextView p5Level;
ImageView p5Epic;
TextView p6Level;
ImageView p6Epic;
TextView tTitle;
private static final int MENU_EPIC_ON = 1;
private static final int MENU_EPIC_OFF = 2;
private static final int MENU_RESET_LEVELS = 3;
private static final int MENU_RESET_NAMES = 4;
private static final int MENU_RESET_ALL = 5;
private static final int MENU_QUIT = 6;
boolean Epic = false;
String Title;
int MaxLevel = 10;
int ilevel = 1;
String slevel = "1";
void ResetLevels(){
for(int x=0;x<6;x++){
Players[x].setLevel(1);
}
p1Level.setText(String.valueOf(Players[0].getPlayerLevel()));
p2Level.setText(String.valueOf(Players[1].getPlayerLevel()));
p3Level.setText(String.valueOf(Players[2].getPlayerLevel()));
p4Level.setText(String.valueOf(Players[3].getPlayerLevel()));
p5Level.setText(String.valueOf(Players[4].getPlayerLevel()));
p6Level.setText(String.valueOf(Players[5].getPlayerLevel()));
p1Epic.setImageResource(R.drawable.black);
p2Epic.setImageResource(R.drawable.black);
p3Epic.setImageResource(R.drawable.black);
p4Epic.setImageResource(R.drawable.black);
p5Epic.setImageResource(R.drawable.black);
p6Epic.setImageResource(R.drawable.black);
}
void SetNames(){
p1text.setText(Players[0].getName());
p2text.setText(Players[1].getName());
p3text.setText(Players[2].getName());
p4text.setText(Players[3].getName());
p5text.setText(Players[4].getName());
p6text.setText(Players[5].getName());
}
void ResetNames(){
for(int x=0;x<6;x++){
Players[x].setName("Player ".concat(String.valueOf(x+1)));
}
p1text.setText(Players[0].getName());
p2text.setText(Players[1].getName());
p3text.setText(Players[2].getName());
p4text.setText(Players[3].getName());
p5text.setText(Players[4].getName());
p6text.setText(Players[5].getName());
}
void ResetAll(){
ResetNames();
ResetLevels();
}
void MinusLevel(Player p){
if (p.getPlayerLevel()>1){
p.setLevel(p.getPlayerLevel()-1);
}
}
void PlusLevel(Player p){
if(p.getPlayerLevel()<MaxLevel){
p.setLevel(p.getPlayerLevel()+1);
}
}
void AskName(final Player p){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
p.setName(value);
SetNames();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Players = new Player[6];
for(int x=0;x<6;x++){
String playername = "Player ".concat(String.valueOf(x+1));
Players[x] = new Player(playername,1,false);
}
if(!savedInstanceState.isEmpty()){ //null pointer exception on start?
Players = (Player[]) savedInstanceState.getParcelableArray("LastPlayerState");
}
p1text = (TextView)this.findViewById(R.id.player_one_name);
p1text.setOnClickListener(this);
Button p1Minus = (Button)this.findViewById(R.id.p1Minus);
p1Minus.setOnClickListener(this);
Button p1Plus = (Button)this.findViewById(R.id.p1Plus);
p1Plus.setOnClickListener(this);
p2text = (TextView)this.findViewById(R.id.player_two_name);
p2text.setOnClickListener(this);
Button p2Minus = (Button)this.findViewById(R.id.p2Minus);
p2Minus.setOnClickListener(this);
Button p2Plus = (Button)this.findViewById(R.id.p2Plus);
p2Plus.setOnClickListener(this);
p3text = (TextView)this.findViewById(R.id.player_three_name);
p3text.setOnClickListener(this);
Button p3Minus = (Button)this.findViewById(R.id.p3Minus);
p3Minus.setOnClickListener(this);
Button p3Plus = (Button)this.findViewById(R.id.p3Plus);
p3Plus.setOnClickListener(this);
p4text = (TextView)this.findViewById(R.id.player_four_name);
p4text.setOnClickListener(this);
Button p4Minus = (Button)this.findViewById(R.id.p4Minus);
p4Minus.setOnClickListener(this);
Button p4Plus = (Button)this.findViewById(R.id.p4Plus);
p4Plus.setOnClickListener(this);
p5text = (TextView)this.findViewById(R.id.player_five_name);
p5text.setOnClickListener(this);
Button p5Minus = (Button)this.findViewById(R.id.p5Minus);
p5Minus.setOnClickListener(this);
Button p5Plus = (Button)this.findViewById(R.id.p5Plus);
p5Plus.setOnClickListener(this);
p6text = (TextView)this.findViewById(R.id.player_six_name);
p6text.setOnClickListener(this);
Button p6Minus = (Button)this.findViewById(R.id.p6Minus);
p6Minus.setOnClickListener(this);
Button p6Plus = (Button)this.findViewById(R.id.p6Plus);
p6Plus.setOnClickListener(this);
p1Level = (TextView)this.findViewById(R.id.p1Level);
p1Epic = (ImageView)this.findViewById(R.id.p1Epic);
p2Level = (TextView)this.findViewById(R.id.p2Level);
p2Epic = (ImageView)this.findViewById(R.id.p2Epic);
p3Level = (TextView)this.findViewById(R.id.p3Level);
p3Epic = (ImageView)this.findViewById(R.id.p3Epic);
p4Level = (TextView)this.findViewById(R.id.p4Level);
p4Epic = (ImageView)this.findViewById(R.id.p4Epic);
p5Level = (TextView)this.findViewById(R.id.p5Level);
p5Epic = (ImageView)this.findViewById(R.id.p5Epic);
p6Level = (TextView)this.findViewById(R.id.p6Level);
p6Epic = (ImageView)this.findViewById(R.id.p6Epic);
}
protected void onPause(){
Bundle b = new Bundle();
b.putParcelableArray("LastPlayerState", Players);
onSaveInstanceState(b);
}
// @Override
public void onClick(View v) {
switch(v.getId()){
case R.id.player_one_name:
AskName(Players[0]);
break;
case R.id.p1Minus:
MinusLevel(Players[0]);
ilevel = Players[0].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p1Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p1Epic.setImageResource(R.drawable.black);
}
p1Level.setText(slevel);
break;
case R.id.p1Plus:
PlusLevel(Players[0]);
ilevel = Players[0].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p1Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p1Epic.setImageResource(R.drawable.black);
}
p1Level.setText(slevel);
break;
case R.id.player_two_name:
AskName(Players[1]);
break;
case R.id.p2Minus:
MinusLevel(Players[1]);
ilevel = Players[1].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p2Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p2Epic.setImageResource(R.drawable.black);
}
p2Level.setText(slevel);
break;
case R.id.p2Plus:
PlusLevel(Players[1]);
ilevel = Players[1].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p2Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p2Epic.setImageResource(R.drawable.black);
}
p2Level.setText(slevel);
break;
case R.id.player_three_name:
AskName(Players[2]);
break;
case R.id.p3Minus:
MinusLevel(Players[2]);
ilevel = Players[2].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p3Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p3Epic.setImageResource(R.drawable.black);
}
p3Level.setText(slevel);
break;
case R.id.p3Plus:
PlusLevel(Players[2]);
ilevel = Players[2].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p3Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p3Epic.setImageResource(R.drawable.black);
}
p3Level.setText(slevel);
break;
case R.id.player_four_name:
AskName(Players[3]);
break;
case R.id.p4Minus:
MinusLevel(Players[3]);
ilevel = Players[3].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p4Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p4Epic.setImageResource(R.drawable.black);
}
p4Level.setText(slevel);
break;
case R.id.p4Plus:
PlusLevel(Players[3]);
ilevel = Players[3].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p4Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p4Epic.setImageResource(R.drawable.black);
}
p4Level.setText(slevel);
break;
case R.id.player_five_name:
AskName(Players[4]);
break;
case R.id.p5Minus:
MinusLevel(Players[4]);
ilevel = Players[4].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p5Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p5Epic.setImageResource(R.drawable.black);
}
p5Level.setText(slevel);
break;
case R.id.p5Plus:
PlusLevel(Players[4]);
ilevel = Players[4].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p5Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p5Epic.setImageResource(R.drawable.black);
}
p5Level.setText(slevel);
break;
case R.id.player_six_name:
AskName(Players[5]);
break;
case R.id.p6Minus:
MinusLevel(Players[5]);
ilevel = Players[5].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p6Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p6Epic.setIma开发者_StackOverflow社区geResource(R.drawable.black);
}
p6Level.setText(slevel);
break;
case R.id.p6Plus:
PlusLevel(Players[5]);
ilevel = Players[5].getPlayerLevel();
slevel = String.valueOf(ilevel);
if(ilevel>=10){
p6Epic.setImageResource(R.drawable.epic);
}
if(ilevel<10){
p6Epic.setImageResource(R.drawable.black);
}
p6Level.setText(slevel);
break;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
tTitle = (TextView)this.findViewById(R.id.title);
menu.add(0, MENU_EPIC_ON, 0, R.string.menu_epic_on);
menu.add(0, MENU_EPIC_OFF, 0, R.string.menu_epic_off);
menu.add(0, MENU_RESET_LEVELS, 0, R.string.menu_reset_levels);
menu.add(0, MENU_RESET_NAMES, 0, R.string.menu_reset_names);
menu.add(0, MENU_RESET_ALL, 0, R.string.menu_reset_all);
menu.add(0, MENU_QUIT, 0, R.string.menu_quit);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_EPIC_ON:
Epic = true;
MaxLevel=20;
tTitle.setText("Epic");
return true;
case MENU_EPIC_OFF:
Epic = false;
MaxLevel=10;
tTitle.setText("Normal");
ResetLevels();
return true;
case MENU_RESET_LEVELS:
ResetLevels();
return true;
case MENU_RESET_NAMES:
ResetNames();
return true;
case MENU_RESET_ALL:
ResetAll();
return true;
case MENU_QUIT:
finish();
return true;
}
return false;
}
}
And here is the code for the custom class
package nicholsoft.LevelCounter;
import android.os.Parcelable;
import android.os.Parcel;
public class Player implements Parcelable{
private String Name;
private int Level;
private boolean Epic;
public Player(String Name, int Level, boolean Epic){
this.setName(Name);
this.Level = Level;
this.Epic = Epic;
}
public void writeToParcel(Parcel out, int flags){
out.writeString(Name);
out.writeInt(Level);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
public Player createFromParcel(Parcel in){ return new Player(in);}
public Player[] newArray(int size){return new Player[size];}
};
private Player(Parcel in){
Name = in.readString();
Level = in.readInt();
}
public Player(){}
public int describeContents() {return 0;}
public String getName(){
return Name;
}
public int getPlayerLevel(){
return Level;
}
public boolean getPlayerEpic(){
return Epic;
}
public void setName(String name){
Name = name;
}
public void setLevel(int level){
Level = level;
}
public void setEpic(boolean epic){
Epic = epic;
}
}
Any help would be appreciated.
From the API documentation for Activity.onCreate(Bundle)
:
savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.
The Bundle can be null and you should probably check for that.
if(savedInstanceState != null && !savedInstanceState.isEmpty()){
Players = (Player[]) savedInstanceState.getParcelableArray("LastPlayerState");
}
精彩评论