2014-02-24 203 views
2

下面的代码将用作定时器。当我的应用程序不加载XML视图时,我正在测试AsynchTask。我的代码应该可以工作,但视图不会打开。我后来做了一些测试,并将TextView变量“display”稍微移动一下,最终得到了显示的视图,但按下开始按钮不起作用......任何人都可以告诉我如何让TextView更新,正常运行?谢谢!从AsyncTask更改TextView

整个布局没有显示....

public class MainActivity extends Activity { 
timer tim = new timer(); 
boolean running = true; 
int milli = 0; 
int secs = 0; 
int mins = 0; 
int hours = 0; 
ArrayList<Lap> laps = new ArrayList<Lap>(); 
TextView display = (TextView) findViewById(R.id.textView1); //whats going on? 
LinearLayout lapsView = (LinearLayout) findViewById(R.id.lapsView); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    try{ 
    Button b1 = (Button) findViewById(R.id.start); 
    b1.setOnClickListener(startTimer); 

    Button b2 = (Button) findViewById(R.id.stop); 
    b2.setOnClickListener(stopTimer); 
    } 
    catch(Exception e1){ 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private OnClickListener startTimer = new OnClickListener() { 
    public void onClick(View v) { 
     if(!running){ 
     startWatch(); 
     } 
     else if(running){ 
      try{ 
      tim.execute(""); //start the task 
      } 
      catch(Exception e1){ 
       Toast.makeText(MainActivity.this, "Error starting: "+e1, Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
}; 

private OnClickListener stopTimer = new OnClickListener() { 
    public void onClick(View v) { 
     if(running){ 
     stopWatch(); 
     } 
    } 
}; 


public void startWatch(){ 
    running = true; 
} 
public void stopWatch(){ 
    running= false; 
} 

public class timer extends AsyncTask<String, Void, String> { 
    ////NOT WORKING!!!! 
    @Override    
protected String doInBackground(String... params) { 
    while(true){ 
     while(!running){ 
      ///Do Nothing... 
     } 
      while(milli < 1000 && running){ 
      running = true; 
      milli++; 
      try{     
       Thread.sleep(1);     
      } 
      catch(Exception e1){ 
       System.out.println(e1); 
      } 
      if(milli == 1000){ 
       milli = 0; 
       secs++;   
      } 
      if(secs == 60){ 
       secs = 0; 
       mins++;    
      } 
      if(mins == 60){ 
       hours++; 
       mins = 0; 
      } 

      display.setText(milli+":"+secs+":"+mins+":"+hours); 
     }    
     } 
    } 
    } 


} 

调试错误....

Thread [<1> main] (Suspended (exception RuntimeException))  
<VM does not provide monitor information> 
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2053  
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2154 
ActivityThread.access$700(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 146  
ActivityThread$H.handleMessage(Message) line: 1260 
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4949  
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
Method.invoke(Object, Object...) line: 511 
ZygoteInit$MethodAndArgsCaller.run() line: 1043 
ZygoteInit.main(String[]) line: 810 
NativeStart.main(String[]) line: not available [native method] 

回答

6

您的AsyncTask无法更新上doInBackground方法的UI。为此,您将从doInBackground拨打publishProgress,并在那里更新TextView

public class timer extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     while(true){ 
      while(milli < 1000 && running){ 
       running = true; 
       milli++; 
       try{ 
        Thread.sleep(1); 
       } 
       catch(Exception e1){ 
        System.out.println(e1); 
       } 
       if(milli == 1000){ 
        milli = 0; 
        secs++; 
       } 
       if(secs == 60){ 
        secs = 0; 
        mins++; 
       } 
       if(mins == 60){ 
        hours++; 
        mins = 0; 
       } 

       this.publishProgress(milli+":"+secs+":"+mins+":"+hours); 
      } 
     } 
    } 
    @Override 
    protected void onProgressUpdate(String... values) { 
     super.onProgressUpdate(values); 
     TextView display = (TextView) findViewById(R.id.textView1); 
       display.setText(values[0]); 
    } 

} 
+0

那么,我该怎么做呢? – Greg

+0

我的答案有样品代码.. –

+0

它的工作原理!谢谢你太多了! – Greg