0
votes

Getting this RuntimeException in android :

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() getting run time error at Toast.makeText(this, latitude+""+longitude, 1).show();

in function 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);
        }

    }
}


}
2

2 Answers

1
votes

You're calling it from a worker thread. You need to call Toast.makeText() from within the main thread. You could use a handler.

0
votes

Sunny's answer is right. You need to use Handler.

Create Handler in your Service.

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

Then pass the message to the Handler like below.

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);
    /*************/
}