为什么警报会收到此错误?

时间:2016-09-14 12:19:49

标签: android gps

我正在构建我正在使用位置的Android应用程序。我的位置在服务中实施。当用户登录时,如果他没有打开GPS,他会收到警告对话框。但是当我退出并再次登陆LoginActivity(我的LAUNCHER活动是MainActivity,而这个GPS的功能是在MainActivity中的onCreate方法)我的应用程序崩溃。 这是我的功能:

 public  void alertForEnablingGPS(){
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            // Build the alertForEnablingGPS dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Location Services Not Active");
            builder.setMessage("Please enable Location Services and GPS");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Show location settings when the user acknowledges the alertForEnablingGPS dialog
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            Dialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }} 

这是我的错误:

09-14 14:19:43.680 7188-7188/com.telnet.asp E/AndroidRuntime: FATAL EXCEPTION: main
                                                          java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                                                              at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:114)
                                                              at android.os.Handler.handleCallback(Handler.java)
                                                              at android.os.Handler.dispatchMessage(Handler.java)
                                                              at android.os.Looper.loop(Looper.java)
                                                              at android.app.ActivityThread.main(ActivityThread.java)
                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                              at java.lang.reflect.Method.invoke(Method.java:525)
                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
                                                              at dalvik.system.NativeStart.main(Native Method)
                                                           Caused by: rx.exceptions.OnCompletedFailedException: Unable to add window -- token android.os.BinderProxy@42c55580 is not valid; is your activity running?
                                                              at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:90)
                                                              at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:284)
                                                              at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
                                                              at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                                                              at android.os.Handler.handleCallback(Handler.java) 
                                                              at android.os.Handler.dispatchMessage(Handler.java) 
                                                              at android.os.Looper.loop(Looper.java) 
                                                              at android.app.ActivityThread.main(ActivityThread.java) 
                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                              at java.lang.reflect.Method.invoke(Method.java:525) 
                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                           Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42c55580 is not valid; is your activity running?
                                                              at android.view.ViewRootImpl.setView(ViewRootImpl.java)
                                                              at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java)
                                                              at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
                                                              at android.app.Dialog.show(Dialog.java)
                                                              at com.telnet.asp.presentation.view.activities.MainActivity.alertForEnablingGPS(MainActivity.java:385)
                                                              at com.telnet.asp.presentation.view.activities.MainActivity.viewAccount(MainActivity.java:142)
                                                              at com.telnet.asp.presentation.presenter.AccountPresenter.showAccountInView(AccountPresenter.java:44)
                                                              at com.telnet.asp.presentation.presenter.AccountPresenter.access$000(AccountPresenter.java:15)
                                                              at com.telnet.asp.presentation.presenter.AccountPresenter$AccountSubscriber.onCompleted(AccountPresenter.java:66)
                                                              at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:84)
                                                              at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:284) 
                                                              at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219) 
                                                              at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107) 
                                                              at android.os.Handler.handleCallback(Handler.java) 
                                                              at android.os.Handler.dispatchMessage(Handler.java) 
                                                              at android.os.Looper.loop(Looper.java) 
                                                              at android.app.ActivityThread.main(ActivityThread.java) 
                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                              at java.lang.reflect.Method.invoke(Method.java:525)

我该怎么做才能解决这个问题? 提前谢谢。

编辑:当我使用Rushi的答案时,我得到了这个错误:

09-14 15:03:11.346 10896-10896/com.telnet.asp E/WindowManager: Activity com.telnet.asp.presentation.view.activities.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42defcf8 V.E..... R.....ID 0,0-720,351} that was originally added here
                                                               android.view.WindowLeaked: Activity com.telnet.asp.presentation.view.activities.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42defcf8 V.E..... R.....ID 0,0-720,351} that was originally added here
                                                                   at android.view.ViewRootImpl.<init>(ViewRootImpl.java)
                                                                   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java)
                                                                   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
                                                                   at android.app.Dialog.show(Dialog.java)
                                                                   at com.telnet.asp.presentation.view.activities.MainActivity.alertForEnablingGPS(MainActivity.java:392)
                                                                   at com.telnet.asp.presentation.view.activities.MainActivity.viewAccount(MainActivity.java:143)
                                                                   at com.telnet.asp.presentation.presenter.AccountPresenter.showAccountInView(AccountPresenter.java:44)
                                                                   at com.telnet.asp.presentation.presenter.AccountPresenter.access$000(AccountPresenter.java:15)
                                                                   at com.telnet.asp.presentation.presenter.AccountPresenter$AccountSubscriber.onCompleted(AccountPresenter.java:66)
                                                                   at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:84)
                                                                   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:284)
                                                                   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
                                                                   at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                                                                   at android.os.Handler.handleCallback(Handler.java)
                                                                   at android.os.Handler.dispatchMessage(Handler.java)
                                                                   at android.os.Looper.loop(Looper.java)
                                                                   at android.app.ActivityThread.main(ActivityThread.java)
                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:525)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
                                                                   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:-1)

GPSTracker gps;
 gps= new GPSTracker(this);
 if (gps.canGetLocation()) {
                    gps= new GPSTracker(Activity.this);
                    latitude = gps.getLatitude();
                    longitude = gps.getLongitude();                 
                }else{
                    gps.showSettingsAlert();
                }

将此单独的类放在您想要使用的位置,初始化此类的对象,并在您的登录活动中编写此行代码。试试这个,让我知道。

DECLARE @tbl1 AS TABLE
(
    ITEM_NBR  VARCHAR(5),
    GRP_NBR VARCHAR(5)
)

INSERT INTO @tbl1 VALUES('0001',221)
INSERT INTO @tbl1 VALUES('0002',221)
INSERT INTO @tbl1 VALUES('0003',221)

答案 1 :(得分:-1)

如果您从后台线程显示对话框并且您的活动已被销毁,您将收到此错误, 在这里找到如何处理来自backgroung线程的对话框,

http://dimitar.me/android-displaying-dialogs-from-background-threads/