2013-04-23 95 views
0

我有以下代碼:爲什麼我的意圖不工作?

public class P1R1 extends Activity { 

    int correctCounter; 
    private String[] answerString; 
    private TextView score; 
    private static final Random rgenerator = new Random(); 
    protected static final String EXTRA_playerOneScore = null; 

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

     score = (TextView) findViewById(R.id.score); 

     correctCounter = 0; 

     loadActivity(); 
     //start round timer 
     new CountDownTimer(60000, 1000) { 

      final TextView timer = (TextView) findViewById(R.id.timer); 


      public void onTick(long millisUntilFinished) { 
       timer.setText("Time: " + millisUntilFinished/1000 + "s"); 
      } 

      public void onFinish() { 
       Intent roundOneToTwo = new Intent(getActivity(), R1R2.class); 
       String playerOneScore = String.valueOf(correctCounter); 
       roundOneToTwo.putExtra(EXTRA_playerOneScore, playerOneScore); 
       startActivity(roundOneToTwo); 
      } 
     }.start(); 

我有一個計時器運行,當計時器結束我希望Intent運行,但我得到一個錯誤,我的意圖說The constructor Intent(new CountDownTimer(){}, Class<R1R2>) is undefined

我確實創建了R1R2類,但是我的意圖仍然給出錯誤。我對android還是個新手,所以如果你能提供幫助,那將會很棒。謝謝!

回答

0

在地方的

Intent roundOneToTwo = new Intent(this, R1R2.class); 

嘗試

Intent roundOneToTwo = new Intent(P1R1.this, R1R2.class); 

第一個參數的構造函數意向應該是上下文,但你提到過的CountDownTimer。請參閱http://developer.android.com/reference/android/content/Intent.html

+0

現在,它給了我錯誤「方法getActivity()未定義爲類型new CountDownTimer(){}」 – 2013-04-23 02:30:27

+0

什麼類/ UI元素實例化CountDownTimer?你是否在某處引用了一個Context或Activity,或許是一個局部變量? – 2013-04-23 02:33:44

+0

如果有幫助,我在代碼中添加了更多內容。 – 2013-04-23 02:36:36