2017-05-29 105 views
-4

我試圖在我的應用程序中實現高分功能,並且我創建了一個int分數變量,但是當我嘗試在另一個類中調用它時,我總是收到「無法解析符號'得分了'」。Android Studio - 無法解析符號

int score = getIntent().getIntExtra("score", 0); 
    scoreLabel.setText(score + ""); 

    SharedPreferences settings = getSharedPreferences("Game_Data", Context.MODE_PRIVATE); 
    int highScore = settings.getInt("High_Score", 0); 

    if(SystemClock.elapsedRealtime() > highScore){ 
     highScoreLabel.setText("High Score : " + score); 

     //save 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putInt("High_Score", score); 
     editor.commit(); 
    }else { 
     highScoreLabel.setText("High Score : " + highScore); 
    } 


} 

然後我在下面的代碼中調用函數,但變量'score'一直顯示爲不能解析符號。我能做什麼?

Intent intent = new Intent(getApplicationContext(), scoreKeeper.class); 
      intent.putExtra("score", score); 
      startActivity(intent); 

請記住我是初學者所以它可能是一些簡單的,我已經錯過了。 謝謝。

+0

確保評分在您的課堂上設置爲全局變量 – ZeroOne

+0

保持變量e分數作爲班級(全球)變量,如果兩個代碼在不同的方法 –

+0

這個問題的任何更新? –

回答

0

在你的字符串,應該有一個佔位符:%1$s

這裏%1是佔位符編號,以及$ S是因爲它是一個字符串。

這裏瞭解更多:在第Stiling Strings格式化字符串

0

設置score爲全局變量

public class MainActivity extends AppCompatActivity { 

    private int score; // set as global 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
       ....... 
    } 
} 

不要忘了刪除線int

int score = getIntent().getIntExtra("score", 0);