[英]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.