【发布时间】:2012-01-22 07:44:20
【问题描述】:
当我的应用程序退出时,我的 GPS 传感器会继续工作,或者当我更改活动时,我的应用程序会根据我的配置崩溃(在下面的代码中,当我更改活动或退出应用程序时它会崩溃)。如何更改我的以下活动,以便当我退出应用程序时 GPS 传感器停止运行,但当我移动到另一个活动时它继续运行?
这是我活动的关键部分:
public class InitialChoice extends Activity {
LocationManager mlocManager;
LocationListener mlocListener;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_screen);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
@Override
protected void onStop(){
//mlocManager.removeUpdates(mlocListener);
mlocManager.removeGpsStatusListener((Listener) mlocListener);
mlocManager = null;
}
}
如果我使用以下代码而不是 onStop(),则 gps 不会停止运行:
@Override
public void finish(){
//mlocManager.removeUpdates(mlocListener);
mlocListener = null;
mlocManager = null;
}
LogCat:
12-16 21:54:58.120: D/LocationManager(909): requestLocationUpdates: provider = gps, listener = com.dummies.android.taskreminder.MyLocationListener@46284458 12-16 21:55:04.740: D/AndroidRuntime(909): 关闭虚拟机 12-16 21:55:04.740: W/dalvikvm(909): threadid=1: 线程以未捕获的异常退出 (group=0x400259f8) 12-16 21:55:04.750:E/AndroidRuntime(909):致命异常:主要 12-16 21:55:04.750:E/AndroidRuntime(909):java.lang.RuntimeException:无法停止活动 {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}:java。 lang.ClassCastException: com.dummies.android.taskreminder.MyLocationListener 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.app.ActivityThread.access$2600(ActivityThread.java:135) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2153) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.os.Handler.dispatchMessage(Handler.java:99) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.os.Looper.loop(Looper.java:144) 12-16 21:55:04.750: E/AndroidRuntime(909): 在 android.app.ActivityThread.main(ActivityThread.java:4937) 12-16 21:55:04.750: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method)
我的新错误来自这里的方法:Stop Location Listener in Android
12-16 23:10:50.295: W/dalvikvm(1272): threadid=1: 线程以未捕获的异常退出 (group=0x400259f8) 12-16 23:10:50.305:E/AndroidRuntime(1272):致命异常:主要 12-16 23:10:50.305:E/AndroidRuntime(1272):java.lang.RuntimeException:无法停止活动 {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}:android。 app.SuperNotCalledException:活动 {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice} 没有调用 super.onStop() 12-16 23:10:50.305: E/AndroidRuntime(1272): 在 android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632) 12-16 23:10:50.305: E/AndroidRuntime(1272): 在 android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)
【问题讨论】:
-
Logcat 崩溃时会说什么?
-
添加了上面的 Logcat。有什么想法吗?