线程因未捕获的异常而退出

时间:2011-02-10 00:04:28

标签: java android

HY!

我的代码:

Thread thread = new Thread (){
            @Override
            public void run() {
                while (true)
                {
                    Handler handler = new Handler(){
                          @Override
                          public void handleMessage(Message msg) {

                             if (msg.obj.toString()!= null)
                             {
                             JSONParse json = null;
                             try {
                                Log.e("Channel_State",msg.obj.toString());
                                 json = new JSONParse(msg.obj.toString());
                                String state = json.getChannelState();
                                id = state;
                                 TextView tv2 = (TextView)findViewById(R.id.mainscreen_state);
                                 tv2.setText("Channel State: "+ state);
                                Log.e("State",state);
                            } catch (final Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                runOnUiThread(new  Runnable() {
                                    public void run() {
                                        Toast toast ;
                                        toast =     Toast.makeText(getApplicationContext(), "Error" + e.getMessage(), 500);
                                        Log.e("Error",e.getMessage());
                                        toast.show();
                                    }
                                });
                            }

                             }
                             else
                             {

                               runOnUiThread(new  Runnable() {
                                public void run() {
                                    Toast toast ;
                                    toast =     Toast.makeText(getApplicationContext(), "error", 500);

                                    toast.show();
                                }
                            });
                             }
                          }
                          };

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("session_id", settings.getString("session_id","error")));
                    params.add(new BasicNameValuePair("channel_id",String.valueOf(settings.getInt("channel_id", -1))));
                    HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelImage", handler);
                    con.start();
                    try {
                        Log.e("Sleep","Begin");
                        this.sleep(5000);
                        Log.e("Sleep","End");

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("Sleep","Fail");
                    }

                }
            }
        };
        thread.start();

日志:

02-10 00:01:29.044: ERROR/AndroidRuntime(1248): Uncaught handler: thread Thread-13 exiting due to uncaught exception
02-10 00:01:29.054: ERROR/AndroidRuntime(1248): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-10 00:01:29.054: ERROR/AndroidRuntime(1248):     at android.os.Handler.<init>(Handler.java:121)
02-10 00:01:29.054: ERROR/AndroidRuntime(1248):     at android.skiptvad.MainScreen$6$1.<init>(MainScreen.java:258)
02-10 00:01:29.054: ERROR/AndroidRuntime(1248):     at android.skiptvad.MainScreen$6.run(MainScreen.java:258)

请帮忙!

3 个答案:

答案 0 :(得分:6)

似乎您想要做的是创建HandlerThread

HandlerThread myThread = new HandlerThread();
myThread.start();
Handler myHandler = new Handler(myThread.getLooper()){
//your handler code.
}

这应该以你想要的方式给你一个由你的处理程序处理的线程。

答案 1 :(得分:2)

您正在工作线程中创建一个处理程序。你不能这样做。处理程序需要在UI线程中实例化。

答案 2 :(得分:1)

在实例化处理程序之前,您需要在线程中启动消息泵。一旦创建了处理程序,它就会绑定到它创建的线程,你可以向它发布消息。线程示例:

class MyThread extends Thread 
   {
      private CountDownLatch mSync = new CountDownLatch(1);
      private Handler mHandler;
      public Handler getHandler()
      {
           try//Make sure the handler has been created.
           {
             mSync.await();
           }
           catch (InterruptedException e)
           {  
               //Do something 
           }
           return mHandler;
      }

      public void run() 
      {
          Looper.prepare();
          
          mHandler = new Handler() 
          {
              public void handleMessage(Message msg) 
              { 
                   if (msg.what == one of your message)
                   {  
                    //....
                   }
              }
          };
          
          mSync.countDown();
          Looper.loop();
      }
  }

使用getHandler()将处理程序返回给您的线程,然后向其发送消息。