等待 GPS 修复对话框抛出错误令牌异常 - Android

发布于 2024-11-28 05:10:29 字数 6938 浏览 3 评论 0原文

我试图显示一个对话框,显示正在等待地图上的 GPS 修复。但它会抛出 BadTokenException。有人可以纠正我的代码吗?我不一定需要一个对话框。它可以是顶部的一条通知,“保持打开状态”,并表示等待 GPS 修复...并在获得修复后消失。请帮我解决这个问题!

package Firstdroid.Gps;
import com.google.android.maps.*;
import Firstdroid.Gps.R;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapViewer extends MapActivity {

    MyLocationOverlay myLocOverlay;
    Location loc;
    Dialog dialog;
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle bundle) 
    {
        super.onCreate(bundle);
        setContentView(R.layout.map);
        initMyLocation();
        mapView = (MapView) findViewById(R.id.mymap);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(19); 
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

    }
    /**
     * Initialises the MyLocationOverlay and adds it to the overlays of the map
     */
    private void initMyLocation() {
        //Runnable to determine when the first GPS fix was received.
        Runnable showWaitDialog = new Runnable() {
            @Override
            public void run() {
            while (loc == null) {
            // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
            }
            };
            // Create a Dialog to let the User know that we're waiting for a GPS Fix
            dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);
            Thread t = new Thread(showWaitDialog);
            t.start();
            while(loc !=null)
            {
            myLocOverlay = new MyLocationOverlay(this, mapView);
            myLocOverlay.enableMyLocation();
            mapView.getOverlays().add(myLocOverlay);
            //MapController mapController = mapView.getController();
            }
    }
    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            mapController.animateTo(point); //  mapController.setCenter(point);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

错误日志:

08-04 06:29:23.794: WARN/dalvikvm(8896): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
08-04 06:29:23.794: WARN/WindowManager(96): Attempted to add window with non-application token WindowToken{463f4b78 token=null}.  Aborting.
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): FATAL EXCEPTION: main
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): java.lang.RuntimeException: Unable to start activity ComponentInfo{Firstdroid.Gps/Firstdroid.Gps.MapViewer}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.os.Looper.loop(Looper.java:144)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.main(ActivityThread.java:4937)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at java.lang.reflect.Method.invoke(Method.java:521)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at dalvik.system.NativeStart.main(Native Method)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.ViewRoot.setView(ViewRoot.java:513)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.Dialog.show(Dialog.java:241)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at Firstdroid.Gps.MapViewer.initMyLocation(MapViewer.java:64)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at Firstdroid.Gps.MapViewer.onCreate(MapViewer.java:37)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     ... 11 more
08-04 06:29:23.814: WARN/ActivityManager(96):   Force finishing activity Firstdroid.Gps/.MapViewer

I'm trying to show a dialog box that says waiting for GPS fix on a map. But it throws BadTokenException. Can some one please correct my code. I not necessarily need a dialog box. It can be a notification on top that 'stays on' and says waiting for gps fix...and disappear once fix is obtained. kindly help me with this!

package Firstdroid.Gps;
import com.google.android.maps.*;
import Firstdroid.Gps.R;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapViewer extends MapActivity {

    MyLocationOverlay myLocOverlay;
    Location loc;
    Dialog dialog;
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle bundle) 
    {
        super.onCreate(bundle);
        setContentView(R.layout.map);
        initMyLocation();
        mapView = (MapView) findViewById(R.id.mymap);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(19); 
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

    }
    /**
     * Initialises the MyLocationOverlay and adds it to the overlays of the map
     */
    private void initMyLocation() {
        //Runnable to determine when the first GPS fix was received.
        Runnable showWaitDialog = new Runnable() {
            @Override
            public void run() {
            while (loc == null) {
            // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
            }
            };
            // Create a Dialog to let the User know that we're waiting for a GPS Fix
            dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);
            Thread t = new Thread(showWaitDialog);
            t.start();
            while(loc !=null)
            {
            myLocOverlay = new MyLocationOverlay(this, mapView);
            myLocOverlay.enableMyLocation();
            mapView.getOverlays().add(myLocOverlay);
            //MapController mapController = mapView.getController();
            }
    }
    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            mapController.animateTo(point); //  mapController.setCenter(point);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

Error Log:

08-04 06:29:23.794: WARN/dalvikvm(8896): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
08-04 06:29:23.794: WARN/WindowManager(96): Attempted to add window with non-application token WindowToken{463f4b78 token=null}.  Aborting.
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): FATAL EXCEPTION: main
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): java.lang.RuntimeException: Unable to start activity ComponentInfo{Firstdroid.Gps/Firstdroid.Gps.MapViewer}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.os.Looper.loop(Looper.java:144)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.main(ActivityThread.java:4937)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at java.lang.reflect.Method.invoke(Method.java:521)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at dalvik.system.NativeStart.main(Native Method)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.ViewRoot.setView(ViewRoot.java:513)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.Dialog.show(Dialog.java:241)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at Firstdroid.Gps.MapViewer.initMyLocation(MapViewer.java:64)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at Firstdroid.Gps.MapViewer.onCreate(MapViewer.java:37)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
08-04 06:29:23.804: ERROR/AndroidRuntime(8896):     ... 11 more
08-04 06:29:23.814: WARN/ActivityManager(96):   Force finishing activity Firstdroid.Gps/.MapViewer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-12-05 05:10:29

您传递给 ProgressDialog 的上下文必须是 Activity,而不是 Application

更改

dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);

为:

dialog = ProgressDialog.show(this, "Please wait...","Retrieving GPS data ...", true);

The context you pass to the ProgressDialog must be an Activity, not an Application.

Change

dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);

to:

dialog = ProgressDialog.show(this, "Please wait...","Retrieving GPS data ...", true);
家住魔仙堡 2024-12-05 05:10:29

从错误看来,您在这里使用了错误的上下文。

dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);

所以,尝试像这样使用它

dialog = ProgressDialog.show(MapViewer.this, "Please wait...","Retrieving GPS data ...", true);

From the error it seems that you are using the wrong context here.

dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);

So, try to use it like this

dialog = ProgressDialog.show(MapViewer.this, "Please wait...","Retrieving GPS data ...", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文