2012-06-03 124 views
1

下面提供的代碼中的朋友,我希望從遊戲意圖恢復時刷新我的文本視圖。但每當我嘗試定義我的TextView出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); 
      } 
     }); 
+0

添加了刷新 – marmor

回答

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

非常感謝你,它的工作以及 –

+0

歡迎朋友!!!,如果它工作,然後接受爲他人的答案幫助尋找同樣的問題。謝謝 –

0
@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 

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

     super.onResume(); 
    } 
+0

崩潰日誌和的onResume代碼現在每當我按下播放按鈕,我的應用程序崩潰, –

3

我開始加入super.onResume();:

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

我也將消除:

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

fromCreate,並將其添加到onResume(),因爲每次調用onCreate時,都會調用onResume。

還從公共更改爲受保護的onResume()