java – 尝试使用Custom Class Array检索savedInstanceState的NullPointerException

我有一个问题,试图让我的程序在屏幕旋转时保持我的变量中的信息.现在它在旋转时重置所有东西.我的信息存储在一个名为Players的自定义类的数组中.我尝试过使用putParcelableArray和getParcelableArray,并在自定义类中使用Implemented Parcelable,并为其提供所需的代码.当我运行代码时,它弹出强制关闭菜单,调试器在我的代码上给出NullPointerException,用于检查savedInstanceState是否为空.

以下是出现问题的主要代码的一部分.

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.setImageResource(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;
        }
}

这是自定义类的代码

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;
    }
}

任何帮助,将不胜感激.

Activity.onCreate(Bundle)的API文档:

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.

Bundle可以为null,你应该检查一下.

if(savedInstanceState != null && !savedInstanceState.isEmpty()){
     Players = (Player[]) savedInstanceState.getParcelableArray("LastPlayerState");
}
相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。