2013-10-06 50 views
0

我發現有兩個按鈕啓動和停止計時器,但不能使其工作 沒有按鈕的活動,我希望它開始自己創建。我試圖把一個可運行的這樣,但它沒有工作計時器沒有按鈕運行本身?

class Starter implements Runnable { 
      public void run() { 
       mCount= 0; 
       timerStart(); 

      } 

    } 

這裏是我的主要活動

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends Activity { 

     TextView mTextView; 
     Button mButtonStart; 
     Button mButtonStop; 

     int  mCount; 
     Timer mTimer; 

     Handler mHandler= new Handler(); 

     Runnable Time= new Runnable() { 
      @Override 
      public void run() { 
       mCount++; 
       mTextView.setText("Count="+mCount); 
      } 
     }; 

     protected class TheTimerTask extends TimerTask { 
      @Override public void run() { 
       // What we want to say is 
       // mCount++; 
       // mTextView.setText("Count="+mCount); 
       // But this gives "Only the original thread that created a view hierarchy can touch its views." 
       mHandler.post(Time); 
      } 
     } 






     protected void timerStart() { 
      if(mTimer==null) { 
       mTimer = new Timer(); 
       mTimer.schedule(new TheTimerTask(), 0, 100); // 100 milli seconds 
      } 
     } 

     protected void timerStop() { 
      if(mTimer!=null) { 
       mTimer.cancel(); 
       mTimer= null; 
      } 
     } 


     OnClickListener mButtonStart_OnClick = new OnClickListener() { 
      @Override public void onClick(View v) { 
       mCount= 0; 
       timerStart(); 
      } 
     }; 

     OnClickListener mButtonStop_OnClick = new OnClickListener() { 
      @Override public void onClick(View v) { 
       timerStop(); 
      } 
     }; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      mTextView= (TextView)findViewById(R.id.textview); 
      mButtonStart= (Button)findViewById(R.id.buttonstart); 
      mButtonStop= (Button)findViewById(R.id.buttonstop); 

      mButtonStart.setOnClickListener(mButtonStart_OnClick); 
      mButtonStop.setOnClickListener(mButtonStop_OnClick); 

     } 
    } 

和我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview" 
    android:text="Press start to start timer"/> 

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonstart" 
    android:text="Start"></Button> 

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Stop" 
     android:id="@+id/buttonstop"></Button> 

</LinearLayout> 
+0

「但它沒有工作」。這是什麼意思? – Simon

+0

如果你不明白它,請考慮問題的其餘部分,嘗試在你的模擬器上運行代碼,看看你是否可以做謝謝 –

+1

沒有人會爲你調試你的代碼。本網站歡迎願意學習的人員,並嘗試解決他們自己的問題。 「它不起作用」可能意味着很多事情。你的應用程序崩潰了?屏幕變黑了?你有ANR錯誤?計時器沒有運行?計時器跑了錯誤的時間?太陽停止閃耀? 「這意味着什麼」是一個完全正確的問題。 – Simon

回答

0

做類似的事情:

private Handler handler = new Handler(); 
handler.postDelayed(runnable, 100); 

private Runnable runnable = new Runnable() { 
@Override 
public void run() { 
    /* do what you need to do */ 
    foobar(); 
    /* and here comes the "trick" */ 
    handler.postDelayed(this, 100); 
} 
}; 

如果您需要更新UI,那麼您可以使用runOnUiThread。你可以找到有關爲什麼使用這種任務處理程序的細節,而不是timerTask here

+0

你也可以看看這個[SO鏈接](http://stackoverflow.com/questions/8678658/handler-or-timer-android) – Mehedi

1

請不要使用定時器/ TimerTasks的創建一個新的線程和你正面臨的問題,使用處理程序代替

+0

您可以與我分享一些鏈接或代碼請 –

+0

以此http://www.google.com/search?q=android+handler+-stackoverflow&btnG=&client=ms-opera-mini-android&channel=new&gws_rd=cr&hl=zh-CN&nfpr開頭=&spell = 1 – pskink