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