无法在未调用Looper.prepare()的线程内创建处理程序

时间:2011-02-14 07:51:42

标签: java android multithreading

HY !!!

我有一个无休止的线程来更新视图。

UpdateState.java

package android.skiptvad;

import java.util.List;

import org.apache.http.NameValuePair;

import android.os.Handler;
import android.os.Message;
import android.text.NoCopySpan.Concrete;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class UpdateState extends Thread {

    public List<NameValuePair> params;
    public Handler handler;

    public UpdateState(List<NameValuePair> params, Handler handler) {
        this.handler = handler;
        this.params = params;
    }

    @Override
    public void run() {

            while (true)
            {
                final Handler h = new Handler (){
                    @Override
                    public void handleMessage(Message msg) {
                         Message msg2 = Message.obtain();
                        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();
                                    msg2.obj = state;
                                    UpdateState.this.handler.sendMessage(msg);
                            } 
                            catch (final Exception e) 
                            {

                                    e.printStackTrace();

                            }
                    }
                }
                };
                    HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelState", h);
                    con.start();
                    try {
                        Log.e("Sleep", "Begin");
                        UpdateState.sleep(5000);
                        Log.e("Sleep", "End");
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("Sleep", "Error");
                        e.printStackTrace();
                    }



            //super.run();
        }
    }
}

拨打

Handler hup = new Handler (){
                @Override
                public void handleMessage(Message msg) {
                    Log.e("UPDATE", (String) msg.obj);
                    if (msg.obj.equals("0")&& msg.obj.equals("1"))
                    {
                        tv2.setText("Channel State: "+ msg.obj);
                    }
                    super.handleMessage(msg);
                }
            };

            UpdateState us = new UpdateState(params, hup);
            us.start();**

日志

02-14 07:45:39.690: ERROR/AndroidRuntime(282): Uncaught handler: thread Thread-13 exiting due to uncaught exception
02-14 07:45:39.690: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-14 07:45:39.690: ERROR/AndroidRuntime(282):     at android.os.Handler.<init>(Handler.java:121)
02-14 07:45:39.690: ERROR/AndroidRuntime(282):     at android.skiptvad.UpdateState$1.<init>(UpdateState.java:30)
02-14 07:45:39.690: ERROR/AndroidRuntime(282):     at android.skiptvad.UpdateState.run(UpdateState.java:30)

如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

错误意味着它所说的内容。您正在new Handler()主题的run()方法中调用UpdateStateHandler用于将消息和runnables注入到Looper的事件队列中,这就是为什么需要在具有准备好的Looper的线程中创建它。你到底想要完成什么?