2017-05-29 70 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);