線程我正在寫一個程序,每5分鐘提取用戶當前的經度和緯度。我使用AlarmManager
,我想運行的程序的背景,所以我用IntentService
。但在執行程序時會出現以下錯誤:引起:java.lang.RuntimeException:不能創建處理程序內部沒有調用Looper.prepare()
logcat的
02-06 15:40:03.101: W/dalvikvm(17499): threadid=12: thread exiting with uncaught exception (group=0x4203c2a0)
02-06 15:40:03.109: E/AndroidRuntime(17499): java.lang.RuntimeException: An error occured while executing doInBackground()
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.lang.Thread.run(Thread.java:856)
02-06 15:40:03.109: E/AndroidRuntime(17499): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.Handler.<init>(Handler.java:121)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:180)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:180)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:657)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:482)
02-06 15:40:03.109: E/AndroidRuntime(17499): at com.gpsshoppe.LocationFinder$CurrentLocatioFinder.doInBackground(LocationFinder.java:58)
02-06 15:40:03.109: E/AndroidRuntime(17499): at com.gpsshoppe.LocationFinder$CurrentLocatioFinder.doInBackground(LocationFinder.java:1)
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-06 15:40:03.109: E/AndroidRuntime(17499): ... 5 more
這是我的課
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
public class LocationFinder extends IntentService {
private Context mcontext;
SharedPreferences shprefs;
public LocationFinder() {
super("LocationFinder");
this.mcontext=this;
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
CurrentLocatioFinder lFinder=new CurrentLocatioFinder();
lFinder.execute();
}
public class CurrentLocatioFinder extends AsyncTask<String, String, String> implements LocationListener{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location!=null){
shprefs=PreferenceManager.getDefaultSharedPreferences(LocationFinder.this);
double latitude=location.getLatitude();
Log.i("Inside cLocation", ""+latitude);
Editor edit=shprefs.edit();
edit.putString("Ltd", ""+latitude);
edit.commit();
double longitude=location.getLongitude();
edit.putString("Lngtd", ""+longitude);
edit.commit();
LatLng lng=new LatLng(latitude,longitude);
Log.i("LatLng", ""+lng);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
你在哪裏初始化了mcontext? –