【问题标题】:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() getting runtime error in androidjava.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序,在 android 中出现运行时错误
【发布时间】:2016-01-13 12:07:44
【问题描述】:

在 android 中获取这个 RuntimeException :

java.lang.RuntimeException:无法在线程内创建处理程序 没有调用 Looper.prepare() 得到运行时错误 Toast.makeText(this, latitude+""+longitude, 1).show();

在函数updateLocation()中。

public class Location_update extends Service {

 double latitude;
 double longitude;
 GPSLocation gps;
 boolean state=true;
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Timer timer = new Timer();
    timer.schedule(task, 0,1000*30);

}

TimerTask task=new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
     updateLocation();
    }
};



public void updateLocation()
{
    gps = new GPSLocation(MainActivity.dis);

    // check if GPS enabled     
    if(gps.canGetLocation()){
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        try
        {
     Toast.makeText(this, latitude+""+longitude, 1).show(); 
        }
        catch(Exception e)
        {
            Log.d("error", "msg:-- "+e);
        }
        // \n is for new line
        //Toast.makeText(Location_update.this, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
    }else{
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }

    if(gps.getLatitude()!=0.0 && gps.getLongitude()!=0.0)
    {
    try {

            String url = "http://surajsingh.freetzi.com/insert_longi_latti.php?latti="+latitude+"&longi="+longitude;
            HttpGet get = new HttpGet(url);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse httpResponse = httpClient.execute(get);
            HttpEntity httpEntity = httpResponse.getEntity();
            String data = EntityUtils.toString(httpEntity);
            JSONObject obj = new JSONObject(data);
            int status = obj.getInt("status");
            }
         catch (Exception e) {
            // TODO: handle exception
            // Toast.makeText(Location_update.this, ""+e, 1).show();
             Log.d("error", ""+e);
        }

    }
}


}

【问题讨论】:

    标签: android runtime-error


    【解决方案1】:

    您从工作线程调用它。您需要从主线程中调用 Toast.makeText()。您可以使用处理程序。

    【讨论】:

      【解决方案2】:

      Sunny 的回答是对的。您需要使用Handler。

      在您的Service 中创建Handler。

      Handler h = new Handler(){
          @Override
          public void handleMessage(Message msg){
              String aResponse = msg.getData().getString("message");
              Toast.makeText(this, aResponse , 1).show(); 
          }
      };
      

      然后将消息传递给Handler,如下所示。

      if(gps.canGetLocation()){
          latitude = gps.getLatitude();
          longitude = gps.getLongitude();
      
          /*************/
          Bundle b = new Bundle();
          b.putString("message", latitude +" "+longitude);
          Message msg = h.obtainMessage();      
          msg.setData(b);
          handler.sendMessage(msg);
          /*************/
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-28
        • 2013-06-27
        相关资源
        最近更新 更多