Android线程异常?

时间:2011-04-07 09:30:42

标签: android android-widget

我在android中遇到了线程异常,我打算做的是,当我点击一个按钮时我开始动态调用处理程序,处理程序用整数值更新文本视图,同时达到整数10,我要停止线程并且必须显示警告,但它会导致错误,我可能会做的事情如下所示

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();

中抛出错误

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

2 个答案:

答案 0 :(得分:1)

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

如果在按钮上单击两次,也会出现异常:您无法在已启动的线程上调用start。您必须创建该Thread的新实例并启动该新实例(如有必要,请停止旧实例)。

您可以看到有关如何创建/停止线程的示例代码的SO答案:Android thread in service issue。我建议你也阅读一些关于Java Threads的教程(这不是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;
         }
     }
}

答案 1 :(得分:0)

这将使线程停在10

    while(counterFlag)
         {

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

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

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