2011-04-07 92 views
0

我得到了android的线程异常,我打算做的是,当我点击一个按钮,我开始一个线程去动态调用处理程序,处理程序更新整数值的文本视图,而达到整数10,我要停止线程,并有显示一个警告,但它会导致一个错误,我可能做的是如下图所示Android线程异常?

public class sample extends Activity implements Runnable{ 

public Camcorder() 
    { 
     try{ 
     counterThread = new Thread(this); 
     }catch(Exception ee) 
     { 

     } 
    } 

    public void run() 
    { 
     try{ 
     while(counterFlag) 
     { 

      System.out.println("The time starts at : "+counter); 

      Thread.sleep(1000); 
      calculate(counter); 
      counter++; 
     } 
     }catch(Exception ee){ 
      System.out.println("Err in ee : "+ee); 
     } 
    } 
     @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      c=this.getApplicationContext(); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      setContentView(R.layout.main); 
         authalert3 = new AlertDialog.Builder(this); 
       authalert3.setTitle("Save Video"); 
       authalert3.setMessage("Do you want to save this Video?"); 
       authalert3.setPositiveButton("Yes", null); 
       Button test = (Button) findViewById(R.id.widget33); 

      test.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
       counter = 0; 
       counterFlag = true; 
       counterThread.start(); 

       } 


}); 

public void calculate(int counter2) { 
     // TODO Auto-generated method stub 

    if(counter2<60){ 
     if(counter2<10) 
     { 
      smin="0"+counter2; 
     } 
     else{ 
      smin=""+counter2; 
     } 
    } 
    else{ 
     hours++; 

     counter=0; 
     smin="00"; 
     if(hours<10){ 
      shours="0"+hours; 
     } 
     else{ 
      shours=""+hours; 
     } 
    } 
     handler.sendEmptyMessage(0); 

    } 


Handler handler = new Handler(){ 
     public void handleMessage(android.os.Message msg) { 
      String tes=shours+":"+smin; 




      time.setText(tes); 
      test(); 
     }; 
    }; 


public void test(){ 
duration=1; 
     if(duration==hours){ 
      counterFlag = false; 
      videoPath=camcorderView.stopRecording(); 
      authalert3.create().show(); 
      counterThread.stop(); 

     } 


    } 

的误差在counterThread.stop抛出();

任何人都建议我,如何解决这个错误。

+0

您需要粘贴错误的异常和调试输出。 – 2011-04-07 09:35:40

回答

1

您不会通过调用counterThread.stop来停止线程。此方法已弃用。在你的情况下,通过设置counterFlag = false;你的线程应该停止。

如果您在按钮上单击两次,您也将收到异常:您无法调用已启动的线程上的开始。您必须创建该线程的新实例并启动该新实例(如果需要,可以先停止旧实例)。

你可以看到,SO回答了关于如何创建/停止线程的一些示例代码:Android thread in service issue。我建议你也阅读一些关于Java线程的教程(这不是特定于Android的)。


此外,我认为你并不需要一个线程在所有的,你正在做什么复杂的,因此,你可以简单地使用处理器来完成所有的工作:

private static final int MSG_REFRESH_UI = 0; 
private static final int MSG_UPDATE_COUNTER = 1; 

private int counter = 0; 

Handler handler = new Handler(){ 
    public void handleMessage(android.os.Message msg) { 
     if (msg.what==MSG_REFRESH_UI) { 
      String tes=shours+":"+smin; 
      time.setText(tes); 
      test(); 
     } else if (msg.what==MSG_UPDATE_COUNTER) { 
      counter++; 
      if (counter<10) { 
       calculate(counter); 
       handler.sendEmptyMessageDelayed(MSG_UPDATE_COUNTER, 1000); 
       handler.sendEmptyMessage(MSG_REFRESH_UI); 
      } 
     } 
    }; 
}; 

public void onResume() { 
    handler.sendEmptyMessage(MSG_UPDATE_COUNTER); 
} 

public void calculate(int counter2) {  
    if (counter2<10) { 
     smin = "0"+counter2; 
    } else if (counter2<60) { 
     smin = ""+counter2; 
    } else{ 
     hours++; 

     counter=0; 
     smin="00"; 
     if(hours<10){ 
      shours="0"+hours; 
     } else { 
      shours=""+hours; 
     } 
    } 
} 
0

这将停止线程为10

while(counterFlag) 
     { 

      System.out.println("The time starts at : "+counter); 

      Thread.sleep(1000); 
      calculate(counter); 
      counter++; 

      if(counter == 10) counterFlag = false; 
     }