2

Hy!!!

I have a endless thread that updated the view.

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

Call:

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

Log:

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)

How to solve my problem?

user547995
  • 2,036
  • 8
  • 33
  • 62
  • Further reading on SO (found after googling the exception message...) : http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare – Andreas Dolk Feb 14 '11 at 08:01
  • ... and another one, same topic, same google: http://stackoverflow.com/questions/3614663/cant-create-handler-inside-thread-that-has-not-called-looper-prepare-inside-as – Andreas Dolk Feb 14 '11 at 08:01

1 Answers1

2

The error means just what it says. You are calling new Handler() inside the run() method of your UpdateState thread. A Handler is used to inject messages and runnables into a Looper's event queue, which is why it needs to be created in a thread that has a prepared Looper. What exactly are you trying to accomplish?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • i want to make a endless thread that download updates from internet every 5 sec. – user547995 Feb 14 '11 at 08:04
  • 1
    Take a look at `Timer` and `TimerTask` (or `ScheduledThreadPoolExecutor` and `Runnable`) for this. – Ted Hopp Feb 14 '11 at 21:12
  • you can just use activity.runOnUiThread(...) that will call the internal handler of the activity – cyrilchampier Aug 02 '13 at 11:52
  • @nerith - As I understood it, OP wants something equivalent to `runOnWorkerThread(...)`. The way to do that is to create a `Handler` while running on the worker thread, which, in turn, requires that a `Looper` be prepared. I should have mentioned that a useful class for doing all this is [`HandlerThread`](http://developer.android.com/reference/android/os/HandlerThread.html). You override `onLooperPrepared()` to do any setup (such as creating a `Handler`) before the looper starts looping. – Ted Hopp Aug 02 '13 at 15:13