我試圖在我的應用程序中實現高分功能,並且我創建了一個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);
請記住我是初學者所以它可能是一些簡單的,我已經錯過了。 謝謝。
確保評分在您的課堂上設置爲全局變量 – ZeroOne
保持變量e分數作爲班級(全球)變量,如果兩個代碼在不同的方法 –
這個問題的任何更新? –