0

所以我很确定我知道我的问题是什么,但我不知道如何解决它或解决它......我正在为一个班级制作这个游戏,现在我正在尝试保存状态当用户单击后退按钮或退出应用程序时。但是当我点击后退按钮时,一切都会重置到你第一次开始的时候..?

这是我的课:

public class PlayScreen extends Activity {

Spinner spinnerMonsters;
Button btnBattle;
TextView textViewBattleResults;
TextView textViewLevelGained;
TextView textViewXPValue;
TextView textViewXpNextLevel;
TextView textViewLevelValue;
TextView textViewGoldValue;
TextView textview5;
TextView textViewStrRace;
TextView textViewStrClass;
TextView textViewStrAlliance;

private int xp = 0;
private int level = 1;
private int gold = 0;
private int hp = 10;
private int mp = 10;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play_screen);

    TextView textview5 = (TextView) findViewById(R.id.textView5);
    TextView textViewStrRace = (TextView) findViewById(R.id.textViewStrRace);
    TextView textViewStrClass = (TextView) findViewById(R.id.TextViewStrClass);
    TextView textViewStrAlliance = (TextView) findViewById(R.id.textViewStrAlliance);

    String Name = getIntent().getStringExtra("strName");
    textview5.setText(Name);
    textview5.setTextColor(Color.CYAN);

    String Race = getIntent().getStringExtra("strRace");
    textViewStrRace.setText(Race);

    String Class = getIntent().getStringExtra("strClass");
    textViewStrClass.setText(Class);

    String Alliance = getIntent().getStringExtra("strAlliance");
    textViewStrAlliance.setText(Alliance);

    Button btnInventory = (Button) findViewById(R.id.button1);
    btnInventory.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), Inventory.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnEquipment = (Button) findViewById(R.id.button2);
    btnEquipment.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), Equipment.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnStats = (Button) findViewById(R.id.button3);
    btnStats.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), Statistics.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnShop = (Button) findViewById(R.id.Button01);
    btnShop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), Shop.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnCrafting = (Button) findViewById(R.id.button4);
    btnCrafting.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), Crafting.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnClassTraining = (Button) findViewById(R.id.button5);
    btnClassTraining.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(v.getContext(), ClassTraining.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnBattle = (Button) findViewById(R.id.btnBattle);
    btnBattle.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method
            // -----------------------------------------------------------------------------------------------------------------------------
            Spinner spinnerMonsters = (Spinner) findViewById(R.id.spinnerMonsters);
            final Button btnBattle = (Button) findViewById(R.id.btnBattle);
            final TextView textViewBattleResults = (TextView) findViewById(R.id.textViewBattleResults);
            TextView textViewXPValue = (TextView) findViewById(R.id.textViewXPValue);
            TextView textViewXpNextLevel = (TextView) findViewById(R.id.textViewXpNextLevel);
            final TextView textViewLevelGained = (TextView) findViewById(R.id.textViewLevelGained);
            TextView textViewLevelValue = (TextView) findViewById(R.id.textViewLevelValue);
            TextView textViewGoldValue = (TextView) findViewById(R.id.textViewGoldValue);
            final TextView textViewXPResults = (TextView) findViewById(R.id.textViewXPResults);
            final TextView textViewGoldResults = (TextView) findViewById(R.id.textViewGoldResults);
            final TextView textViewLootResults = (TextView) findViewById(R.id.textViewLootResults);
            TextView textViewHP = (TextView) findViewById(R.id.textViewHP);
            TextView textViewMP = (TextView) findViewById(R.id.textViewMP);
            final TextView textViewTimer = (TextView) findViewById(R.id.textView10);
            int XP = Integer.parseInt(textViewXPValue.getText().toString());
            int XPNEXT = Integer.parseInt(textViewXpNextLevel.getText()
                    .toString());
            // -----------------------------------------------------------------------------------------------------------------------------

            // ---------------------
            // XPValue <= XPLimit |
            // ---------------------

            if (textViewXPValue.getText().toString()
                    .equals(textViewXpNextLevel.getText().toString())) {
                int newLimit = Integer.parseInt(textViewXpNextLevel
                        .getText().toString()) + 100;
                textViewXpNextLevel.setText(newLimit + "");
                textViewLevelValue.setText(String.valueOf(level));
                textViewHP.setText(String.valueOf(hp));
                textViewMP.setText(String.valueOf(mp));
                textViewLevelGained.setVisibility(View.VISIBLE);
                textViewLevelGained.setText("You have gained a level!");
                xp = 0;
                level = level + 1;
                hp = hp + 10;
                mp = mp + 10;
            } else if (XP >= XPNEXT) {
                int newLimit = Integer.parseInt(textViewXpNextLevel
                        .getText().toString()) + 100;
                textViewXpNextLevel.setText(newLimit + "");
                textViewLevelValue.setText(String.valueOf(level));
                textViewHP.setText(String.valueOf(hp));
                textViewMP.setText(String.valueOf(mp));
                textViewLevelGained.setVisibility(View.VISIBLE);
                textViewLevelGained.setText("You have gained a level!");
                xp = 0;
                level = level + 1;
                hp = hp + 10;
                mp = mp + 10;
            }

            if (level == 51) {
                textViewXPValue.setText(textViewXpNextLevel.getText()
                        .toString());
                textViewBattleResults.setText("Thanks for playing!");
                level = level + 0;
                hp = hp + 0;
                mp = mp + 0;
                xp = xp + 0;
            }

            // -----------------------------------------------------------------------------------------------------------------------------

            // ----------------------------
            // Count Down Timer Activity |
            // ----------------------------

            new CountDownTimer(1000, 1000) {

                public void onTick(long millisUntilFinished) {
                    textViewTimer.setText("" + millisUntilFinished / 1000);
                    textViewXPResults.setVisibility(View.VISIBLE);
                    textViewGoldResults.setVisibility(View.VISIBLE);
                    textViewLootResults.setVisibility(View.VISIBLE);
                }

                public void onFinish() {
                    textViewTimer.setVisibility(View.INVISIBLE);
                    textViewBattleResults.setVisibility(View.INVISIBLE);
                    textViewXPResults.setText("XP: ");
                    textViewGoldResults.setText("Gold: ");
                    textViewLootResults.setText("Loot: ");
                    btnBattle.setVisibility(View.VISIBLE);
                    textViewLevelGained.setVisibility(View.INVISIBLE);

                }
            }.start();

            textViewTimer.setVisibility(View.VISIBLE);
            btnBattle.setVisibility(View.INVISIBLE);

            // -----------------------------------------------------------------------------------------------------------------------------

            // --------------------
            // Monster sequences |
            // --------------------

            // Training Dummy
            if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(0) Training Dummy")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                } else {
                    textViewBattleResults
                            .setText("You have killed Training Dummy!");
                    xp = xp + 100;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewXPResults.setText("XP: +10");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Cockroach
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(2) Cockroach")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                } else if (level >= 2) {
                    textViewBattleResults
                            .setText("You have killed a Cockroach!");
                    xp = xp + 27;
                    gold = gold + 3;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +27");
                    textViewGoldResults.setText("Gold: +3");
                    textViewLootResults.setText("Loot: none");

                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Cockroach!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Diseased Rat
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(3) Diseased Rat")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level >= 3) {
                    textViewBattleResults
                            .setText("You have killed a Diseased Rat!");
                    xp = xp + 36;
                    gold = gold + 5;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +36");
                    textViewGoldResults.setText("Gold: +5");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Diseased Rat!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Black Widdow
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(5) Black Widdow")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level >= 5) {
                    textViewBattleResults
                            .setText("You have killed a Black Widdow!");
                    xp = xp + 45;
                    gold = gold + 7;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +45");
                    textViewGoldResults.setText("Gold: +7");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Black Widdow!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Goblin
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(7) Goblin")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level >= 7) {
                    textViewBattleResults
                            .setText("You have killed a Goblin!");
                    xp = xp + 51;
                    gold = gold + 11;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +51");
                    textViewGoldResults.setText("Gold: +11");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Goblin!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Undead Troll
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(9) Undead Troll")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level >= 9) {
                    textViewBattleResults
                            .setText("You have killed an Undead Troll!");
                    xp = xp + 63;
                    gold = gold + 19;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +63");
                    textViewGoldResults.setText("Gold: +19");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by an Undead Troll!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Giant
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(13) Giant")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 13) {
                    textViewBattleResults
                            .setText("You have killed a Giant!");
                    xp = xp + 72;
                    gold = gold + 28;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +72");
                    textViewGoldResults.setText("Gold: +28");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have defeated killed by a Giant!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Young Blue Drake
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(17) Young Blue Drake")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 17) {
                    textViewBattleResults
                            .setText("You have killed a Young Blue Drake!");
                    xp = xp + 88;
                    gold = gold + 42;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +88");
                    textViewGoldResults.setText("Gold: +42");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Young Blue Drake!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Hobgoblin
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(21) Hobgoblin")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 21) {
                    textViewBattleResults
                            .setText("You have killed a Hobgoblin!");
                    xp = xp + 94;
                    gold = gold + 51;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +94");
                    textViewGoldResults.setText("Gold: +51");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Hobgoblin!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Skeletion
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(23) Skeleton")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 23) {
                    textViewBattleResults
                            .setText("You have killed a Skeleton!");
                    xp = xp + 101;
                    gold = gold + 60;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +101");
                    textViewGoldResults.setText("Gold: +60");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Skeleton!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Thief Master
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(26) Thief Master")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 26) {
                    textViewBattleResults
                            .setText("You have killed a Thief Master!");
                    xp = xp + 117;
                    gold = gold + 74;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +117");
                    textViewGoldResults.setText("Gold: +74");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Thief Master!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }// Homunculus
            else if (spinnerMonsters.getSelectedItem().toString()
                    .equals("(30) Homunculus")) {
                textViewBattleResults.setVisibility(View.VISIBLE);
                if (level == 51) {
                    xp = xp + 0;
                }
                if (level > 30) {
                    textViewBattleResults
                            .setText("You have killed a Homunculus!");
                    xp = xp + 121;
                    gold = gold + 82;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: +121");
                    textViewGoldResults.setText("Gold: +82");
                    textViewLootResults.setText("Loot: none");
                } else {
                    textViewBattleResults
                            .setText("You have been defeated by a Homunculus!");
                    xp = xp + 0;
                    gold = gold + 0;
                    textViewXPValue.setText(String.valueOf(xp));
                    textViewGoldValue.setText(String.valueOf(gold));
                    textViewXPResults.setText("XP: 0");
                    textViewGoldResults.setText("Gold: 0");
                    textViewLootResults.setText("Loot: none");
                }
            }

        }
    }); /* Ends the button onClickListener */
}
}

现在我很确定问题出在我把所有私人 int 放在哪里,当你第一次开始玩时,它们都等于 0 或其他任何值。我好失落...

我删除了我的 onPause 和 onResume 事件,因为它们只是不工作,这是我尝试之前的代码。

4

2 回答 2

0

您的所有变量在运行时都有状态。如果您关闭您的应用程序每个分配的内存并且变量中存储的值也会丢失。例如,您必须将这些内容保存在 SQLite 数据库或设备上的 xml 文件中,您可以从中获取值。

阅读有关 android 存储的本教程可能会有所帮助: 存储选项

于 2012-09-29T21:29:17.483 回答
0

您可以将应用程序的值保存在 onPause() 中,例如:

Editor editor = getSharedPreferences("myStatusFile", MODE_PRIVATE).edit();
editor.clear();
editor.putString("string1", string1);
editor.putInteger("integer1", integer1);
editor.commit();

并在 onResume() 中恢复它们:

SharedPreferences preferences = getSharedPreferences("myStatusFile", MODE_PRIVATE);
string1 = preferences.getString("string1",  null);
integer1 = preferences.getString("integer1",  0);

祝你好运

于 2012-09-29T23:04:38.353 回答