1

下面提供的这段代码中的朋友,我想在从播放意图恢复时刷新我的文本视图。但是,每当我尝试在 OnCreate 之外但在我的主类中定义我的 textview 时(在静态 int 分数之后),我的应用程序就会崩溃。

public class MainProjectActivity extends Activity {
    /** Called when the activity is first created. */

    static int Score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Display Scores
        final TextView displayScores = (TextView)findViewById(R.id.scoreDisplay);
        displayScores.setText("Your Score : "+ Score);

        //Play Game button activity
        Button gameButton = (Button)findViewById(R.id.PlayButton);
        gameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
                startActivity(play);
            }
        });
4

3 回答 3

3

我将从添加 super.onResume(); 开始:

@Override
protected void onResume(){
    super.onResume();
    // The rest
}

我也会删除:

final TextView displayScores = (TextView)findViewById(R.id.scoreDisplay);
        displayScores.setText("Your Score : "+ Score);

从 onCreate 开始,并将其添加到 onResume(),因为每次调用 onCreate 时,也会调用 onResume。

也从公共更改为受保护的 onResume()

于 2012-06-03T11:40:18.210 回答
1

尝试这个:

public class MainProjectActivity extends Activity {
    /** Called when the activity is first created. */
TextView displayScores;
    static int Score = 0;
@Override
protected void onResume(){
    super.onResume();
    // code to update the date here
    displayScores.setText("Your Score : "+ Score);

}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Display Scores
        displayScores = (TextView)findViewById(R.id.scoreDisplay);
        displayScores.setText("Your Score : "+ Score);

        //Play Game button activity
        Button gameButton = (Button)findViewById(R.id.PlayButton);
        gameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
                startActivity(play);
            }
        });
于 2012-06-03T11:37:10.003 回答
0
@Override
    protected void onResume() {
        // TODO Auto-generated method stub

        displayScores.setText("Your Score : "+ Score);

        super.onResume();
    }
于 2012-06-03T11:21:12.047 回答