Android 在恢复主要活动时更新TextView

Android 在恢复主要活动时更新TextView,android,android-intent,textview,android-activity,onresume,Android,Android Intent,Textview,Android Activity,Onresume,在下面提供的代码中,我想在从play intent恢复时刷新我的文本视图。但每当我试图在OnCreate之外,但在静态int分数之后在主类内部定义textview时,我的应用程序就会崩溃 public class MainProjectActivity extends Activity { /** Called when the activity is first created. */ static int Score = 0; @Override prot

在下面提供的代码中,我想在从play intent恢复时刷新我的文本视图。但每当我试图在OnCreate之外,但在静态int分数之后在主类内部定义textview时,我的应用程序就会崩溃

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);
            }
        });
试试这个:

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);
            }
        });
试试这个:

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

我首先要添加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


另外,从public改为protected onResume

我首先要添加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


还可以从public更改为protected onResume

为刷新添加崩溃日志和onResume代码为刷新添加崩溃日志和onResume代码欢迎朋友!!!,如果它起作用,那么接受作为其他人的答案帮助寻找相同的问题。谢谢你的朋友!!!,如果它是工作,然后接受作为其他人的答案帮助寻找相同的问题。谢谢