I had a problem trying to get my program to store information in my variables when the screen rotates. Now it resets everything when it spins. My information is stored in an array of the custom class Players. I tried using putParcelableArray and getParcelableArray and implemented Parcelable in a custom class and the code needed for it. When I run the code, it opens a force close menu, and the debugger gives a NullPointerException in my code to check if savedInstanceState is empty.
Here is the 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(); } @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()){
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.
Bazui source share