开发者

Thread exiting due uncaught exception

Hy!

My code:

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","Fai开发者_开发知识库l");
                    }

                }
            }
        };
        thread.start();

Log:

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)

Please help!


Seems like what you want to be doing is creating a HandlerThread

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

This should give you a thread handled by your handler in the way you are looking for.


You're creating a Handler in a worker thread. You can't do that. The handler needs to be instantiated in the UI thread.


You need to start a message pump in the thread before instantiating a handler. Once the handler is created it's bound to the thread it's created in and you can post messages to it. Example of the thread:

 

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

Use getHandler() to return the handler to your thread, then send messages to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜