我正在使用异步任务并搜索我的 gps 位置(使用网络提供商)我收到错误locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
public class SimpleDirect extends MapActivity {
MapView mapView;
private ProgressDialog pDialog;
private LocationManager locManager;
private LocationListener locListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_direct_main);
mapView = (MapView)findViewById(R.id.mapView);
initMap();
new Activity().execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class Activity extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SimpleDirect.this);
pDialog.setMessage("Mohon tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
initLocationManager();
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
private void initLocationManager(){
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new LocationListener() {
public void onLocationChanged(Location newLocation) {
tampilkanPosisikemap(newLocation);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0){
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
protected void tampilkanPosisikemap(Location newLocation) {
List overlays = mapView.getOverlays();
if(overlays.size() > 0) {
for(Iterator iterator = overlays.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
}
}
GeoPoint geopoint = new GeoPoint(
(int) (newLocation.getLatitude() * 1E6), (int) (newLocation
.getLongitude() * 1E6));
Drawable icon = getResources().getDrawable(R.drawable.marker);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon
.getIntrinsicHeight());
MyItemizedOverlay overlay = new MyItemizedOverlay(icon);
OverlayItem item = new OverlayItem(geopoint, "My Location", null);
overlay.addItem(item);
mapView.getOverlays().add(overlay);
mapView.getController().animateTo(geopoint);
mapView.postInvalidate();
}
private void initMap() {
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.getController().setZoom(15);
}
@Override
protected boolean isRouteDisplayed(){
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.simple_direct_main, menu);
return true;
}
}
我的日志猫:
04-22 20:48:26.763: E/AndroidRuntime(15676): FATAL EXCEPTION: AsyncTask #1
04-22 20:48:26.763: E/AndroidRuntime(15676): java.lang.RuntimeException: An error occured while executing doInBackground()
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.lang.Thread.run(Thread.java:1096)
04-22 20:48:26.763: E/AndroidRuntime(15676): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.os.Handler.<init>(Handler.java:121)
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
04-22 20:48:26.763: E/AndroidRuntime(15676): at com.example.simpledirection.SimpleDirect.initLocationManager(SimpleDirect.java:128)
04-22 20:48:26.763: E/AndroidRuntime(15676): at com.example.simpledirection.SimpleDirect.access$2(SimpleDirect.java:107)
04-22 20:48:26.763: E/AndroidRuntime(15676): at com.example.simpledirection.SimpleDirect$Activity.doInBackground(SimpleDirect.java:93)
04-22 20:48:26.763: E/AndroidRuntime(15676): at com.example.simpledirection.SimpleDirect$Activity.doInBackground(SimpleDirect.java:1)
04-22 20:48:26.763: E/AndroidRuntime(15676): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-22 20:48:26.763: E/AndroidRuntime(15676): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-22 20:48:26.763: E/AndroidRuntime(15676): ... 4 more
04-22 20:48:26.813: E/MapActivity(15676): Couldn't get connection factory client
04-22 20:48:27.623: E/WindowManager(15676): Activity com.example.simpledirection.SimpleDirect has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a0aa048 that was originally added here
04-22 20:48:27.623: E/WindowManager(15676): android.view.WindowLeaked: Activity com.example.simpledirection.SimpleDirect has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a0aa048 that was originally added here
04-22 20:48:27.623: E/WindowManager(15676): at android.view.ViewRoot.<init>(ViewRoot.java:247)
04-22 20:48:27.623: E/WindowManager(15676): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-22 20:48:27.623: E/WindowManager(15676): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-22 20:48:27.623: E/WindowManager(15676): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.Dialog.show(Dialog.java:241)
04-22 20:48:27.623: E/WindowManager(15676): at com.example.simpledirection.SimpleDirect$Activity.onPreExecute(SimpleDirect.java:88)
04-22 20:48:27.623: E/WindowManager(15676): at android.os.AsyncTask.execute(AsyncTask.java:391)
04-22 20:48:27.623: E/WindowManager(15676): at com.example.simpledirection.SimpleDirect.onCreate(SimpleDirect.java:59)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.ActivityThread.access$2300(ActivityThread.java:126)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
04-22 20:48:27.623: E/WindowManager(15676): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 20:48:27.623: E/WindowManager(15676): at android.os.Looper.loop(Looper.java:123)
04-22 20:48:27.623: E/WindowManager(15676): at android.app.ActivityThread.main(ActivityThread.java:4633)
04-22 20:48:27.623: E/WindowManager(15676): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 20:48:27.623: E/WindowManager(15676): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 20:48:27.623: E/WindowManager(15676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-22 20:48:27.623: E/WindowManager(15676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-22 20:48:27.623: E/WindowManager(15676): at dalvik.system.NativeStart.main(Native Method)