2012-09-29 80 views
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 */ 
} 
} 

現在,我敢肯定,問題出在哪裏我把我所有的私人詮釋的,他們都等於0或任何當你第一次開始播放。我迷茫......

我刪除了我的onPause和活動的onResume因爲他們只是沒有工作,這是代碼之前,我嘗試過這一點。

+0

ŧ他在Android上是相當基本的問題。看看http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Dayerman

回答

0

所有的變量在運行時都有狀態。如果關閉應用程序,則每個分配的內存以及變量中的值存儲都會丟失。例如,您必須在設備上的SQLite數據庫或xml文件中保存這些內容,以便從中獲取值。

它可幫助瞭解存儲這個教程在Android: Storage Options

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

好運