2013-12-19 44 views
-1

所以我有一個非常基本的問題。我的代碼基本上是這樣的Java代碼:更改變量值

用戶檢查一個無線電按鈕選擇一個時間(他們有1小時,2和3),然後點擊從Activity1的按鈕,然後他們被帶到Activity2,它有一個TextView中顯示的時間不多了以秒

我希望部分是清楚現在

,TextView的在活性2應該會顯示來自活性1路過的傳遞時間在這條線從活動1

時間intent.putExtra(「Time」,time);

每當我嘗試這個代碼,結果我得到的是始終爲0,好像根據它是不會改變的時間值我if語句我已經包括了每一個單選按鈕器isChecked塊內。那麼,幫忙?我知道必須有一個非常簡單和容易的解決方案。

謝謝。

protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.release_bicycle); 
     one = (RadioButton)findViewById(R.id.oneHour); 
     two = (RadioButton)findViewById(R.id.twoHours); 
     three = (RadioButton)findViewById(R.id.threeHours); 
     Go = (Button)findViewById(R.id.releaseBTN); 


     Go.setOnClickListener(new View.OnClickListener() 
    {   
     @Override 
public void onClick(View v) 
{ 

    //start bluetooth 

     if (one.isChecked()) 
     { 
      new CountDownTimer(3600000, 1000) 
      { 
       public void onTick(long millisUntilFinished) 
       { 
        time = (millisUntilFinished/1000); 
       } 

       public void onFinish() 
       { 
        ... database 
       } 
      }.start(); 

     } 


     if (two.isChecked()) 
     { 
        ... 
     } 


     if (three.isChecked()) 
     {   
      ... 

     } 


     Intent intent = new Intent(current.this, destination.class); 
     intent.putExtra ("Time", time); 
     startActivity(intent);  
    } 

    }); 


} 
+2

我把這個行的'time'的值:'intent.putExtra(「Time」,time);'是0? –

+0

@AleksanderLidtke是 –

+2

爲什麼不在Activity2中啓動倒數計時器? – njzk2

回答

0

public void onTick(long millisUntilFinished)似乎被稱爲後您的onCreate()結束。

你應該把下面的代碼在調用onStart例如

Intent intent = new Intent(current.this, destination.class); 
intent.putExtra ("Time", time); 
startActivity(intent); 

這是一個奇怪的方法來創建只啓動另一個活動的活動在onStart甚至上創建。

您可以做的另一個選擇是,使用處理程序執行延遲後執行該代碼。

+0

這段代碼在我的onCeate()中,我沒有粘貼整個代碼。 –