2013-09-25 22 views
0

我只是繼續說,我的問題可能是愚蠢的。它可能以前也被回答過,或者很明顯它不需要回答,但我似乎無法找到合適的詞組合來找到合適的解決方案。 我沒有編程很久,所以我可能只是生鏽,但我真的很難得出這個錯誤。 我在寫一個android應用程序,它使用一個計時器來計算你正在製作多少錢。你輸入小時工資,開始計時,它應該每秒更新一次,顯示你已經做了多少。但是,我無法識別計時器任務中的TextView對象,因此我從編譯器中得到一個丟失的符號錯誤。runinuithread找不到文本視圖

以下是用戶輸入工資的主要活動。

public class IncomeCounter extends Activity 
    { 
     public final static String EXTRA_MESSAGE = "com.altotech.incomecounter.MESSAGE"; 
     /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    public void sendMessage(View view){ 
     Intent intent = new Intent(this, Clock.class); 
     EditText editText = (EditText) findViewById(R.id.edittextstring); 
     String enteredText = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, enteredText); 
     startActivity(intent); 

    } 
} 

這裏就是計時器應該開始計數

public class Clock extends Activity{ 
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    //get intent from IncomeCounter 
    Intent intent = getIntent(); 
    Bundle bundle = new Bundle(); 

    //create textview 
    TextView textview = new TextView(this); 
    textview.setTextSize(40); 
    setContentView(textview); 


    //initialize currentIncome, put in bundle 
    double currentIncome = 0; 
    bundle.putDouble("current", currentIncome); 

    //create and start timer 
    MyTimerTask myTask = new MyTimerTask(); 
    Timer myTimer = new Timer(); 
    myTimer.schedule(myTask, 0, 1000); 

    } 

    class MyTimerTask extends TimerTask{ 
     public void run() { 
      runOnUiThread(new Runnable(){ 
       public void run(){ 

       //get intent and variables 
       Intent intent = getIntent(); 
       Bundle bundle = intent.getExtras(); 

       //scale incomeRate down to incomePerSecond, get currentIncome and add incomePerSecond to it, putDouble(currentincome), draw it onscreen, then repeat 
       String incomeRate = intent.getStringExtra(IncomeCounter.EXTRA_MESSAGE); 
       double incomePerSecond = (Double.parseDouble(incomeRate)/3600); 
       double currentIncome = bundle.getDouble("current"); 
       currentIncome = currentIncome + incomePerSecond; 
       bundle.putDouble("current",currentIncome); 

       textview.setText(Double.valueOf(currentIncome).toString()); 

      } 
     }); 

     } 
    } 

} 

textview.setText(...);行是次活動,其中的問題的。只是,每當我看到其他使用runinuithread的定時器的例子時,他們似乎都能夠順利地完成setText。也許我在誤讀,或者我只是啞巴。但是,感謝您花時間閱讀我的問題和粗糙的代碼。

回答

1

嘗試通過在onCreate()或任何其他方法之外聲明textview成員變量。

public class Clock extends Activity{ 
@SuppressLint("NewApi") 

TextView textview; // declare it here 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

//get intent from IncomeCounter 
Intent intent = getIntent(); 
Bundle bundle = new Bundle(); 

//create textview 
textview = new TextView(this); // and initialize it here 
+0

謝謝,它的工作。你真棒。現在我已經運行了,我發現textview並不是實際上每秒更新一次,或者currentIncome實際上並沒有增加。不過,我想我可以從這裏得到它自己。再次感謝。 – user2816570

+0

不客氣。祝你好運!並努力提高你的信心;) – codeMagic