简体   繁体   English

Android致命异常:无法在未调用Looper.prepare()的线程内创建处理程序

[英]Android fatal exception: Can't create handler inside thread that has not called Looper.prepare()

I have read similar questions about this problem and tried to find a solution but I have not figure out what is the reason in my case. 我已经阅读了关于这个问题的类似问题,并试图找到一个解决方案,但我还没弄清楚我的情况是什么原因。 My program's code rough is below : 我的程序代码粗略如下:

public class ReportSystem extends Activity implements SensorEventListener , Runnable{ 

    ReportLocation reportObj = new ReportLocation(this); //my other class
    Thread thread_send = new Thread(this);

    Handler handler = new Handler() {
        public void handleMessage(Message message) {    
            msg.setText("---"));
        }
    };

    public void onCreate(Bundle savedInstanceState){ 
    //something...
    }

    public void onSensorChanged(SensorEvent event){ 

        if(event.values[0] > 10)
            thread_send.start(); // thread is started..
    }

    public void run(){

        reportObj.send(); //connect with server and send data by the help of RepotLocation class' send function
        handler.sendEmptyMessage(0);
    }
}//end class

What should I do to solve this problem ? 我该怎么做才能解决这个问题?

Try this: 尝试这个:

public void run(){
    Looper.prepare();
    reportObj.send();
    handler.sendEmptyMessage(0);
    Looper.loop();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android空指针异常,并且无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() Asynctask导致异常“无法在未调用Looper.prepare()的线程内创建处理程序” - Asynctask causes exception 'Can't create handler inside thread that has not called Looper.prepare()' 无法在未调用looper.prepare异常的线程内创建处理程序 - Can't create handler inside thread that has not called looper.prepare exception 运行时异常“无法在未调用looper.prepare()的线程内创建处理程序” - Runtime exception “can't create handler inside thread that has not called looper.prepare()” 无法在aynctask中未调用Looper.prepare()异常的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Exception in aynctask 运行时异常无法在未调用looper.prepare错误的线程内创建处理程序 - runtime exception can't create handler inside thread that has not called looper.prepare error 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android Android无法在未调用looper.prepare()的线程内创建处理程序 - Android can't create handler inside thread that has not called looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM