等待GPS定位对话框抛出坏令牌例外 - Android电子 [英] Waiting for GPS fix dialog box throws Bad token Exception - Android
本文介绍了等待GPS定位对话框抛出坏令牌例外 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要表明说,一个地图上等待GPS定位的对话框。但它抛出BadTokenException。可有一个人,请纠正我的code。我并不一定需要的对话框。它可以在顶部的通知岿然不动,并表示等待GPS定位......而消失,一旦修复获得。好心帮我看看这个!
包Firstdroid.Gps;
进口com.google.android.maps *。
进口Firstdroid.Gps.R;
进口android.app.Dialog;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.location.Location;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
公共类的MapViewer扩展MapActivity { MyLocationOverlay myLocOverlay;
位置禄;
对话的对话;
私人MapController mapController;
私人MapView类MapView类;
私人的LocationManager的LocationManager;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑包)
{
super.onCreate(包);
的setContentView(R.layout.map);
initMyLocation();
图形页面=(图形页面)findViewById(R.id.mymap);
mapView.setBuiltInZoomControls(真);
mapView.setStreetView(真);
mapController = mapView.getController();
mapController.setZoom(19);
的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,
0,新GeoUpdateHandler()); }
/ **
*初始化的MyLocationOverlay并将其添加到地图的覆盖
* /
私人无效initMyLocation(){
// Runnable接口来确定用户收到的第一个GPS定位的时候。
可运行showWaitDialog =新的Runnable(){
@覆盖
公共无效的run(){
而(LOC == NULL){
//等待第一个GPS定位(什么也不做,直到LOC!= NULL)
}
//接收后第一个GPS定位驳回进度对话框
dialog.dismiss();
}
};
//创建一个对话框,让用户知道我们正在等待GPS定位
对话框= ProgressDialog.show(getApplicationContext(),请稍候...,检索GPS数据......,真);
线程t =新主题(showWaitDialog);
t.start();
而(LOC!= NULL)
{
myLocOverlay =新MyLocationOverlay(这一点,MapView类);
myLocOverlay.enableMyLocation();
。调用MapView.getOverlays()加(myLocOverlay);
// MapController mapController = mapView.getController();
}
}
公共类GeoUpdateHandler实现LocationListener的{ @覆盖
公共无效onLocationChanged(地点){
INT纬度=(INT)(location.getLatitude()* 1E6);
INT LNG =(INT)(location.getLongitude()* 1E6);
的GeoPoint点=新的GeoPoint(纬度,经度);
mapController.animateTo(点); // mapController.setCenter(点);
} @覆盖
公共无效onProviderDisabled(字符串提供商){
} @覆盖
公共无效onProviderEnabled(字符串提供商){
} @覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
}
} @覆盖
保护布尔isRouteDisplayed(){
返回false;
}
}
错误日志:
6月8日至4日:29:23.794:WARN / dalvikvm(8896):主题ID = 1:螺纹未捕获的异常(组= 0x400259f8)退出
6月8日至4日:29:23.794:WARN /窗口管理器(96):试图用非应用程序令牌WindowToken {463f4b78令牌= NULL}添加窗口。中止。
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):致命异常:主要
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):了java.lang.RuntimeException:无法启动活动ComponentInfo {Firstdroid.Gps / Firstdroid.Gps.MapViewer}:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread.access $ 2300(ActivityThread.java:135)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.os.Handler.dispatchMessage(Handler.java:99)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.os.Looper.loop(Looper.java:144)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread.main(ActivityThread.java:4937)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在java.lang.reflect.Method.invokeNative(本机方法)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在java.lang.reflect.Method.invoke(Method.java:521)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在dalvik.system.NativeStart.main(本机方法)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):android.view.WindowManager $ BadTokenException:产生的原因无法添加窗口 - 令牌null不是一个应用程序
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.view.ViewRoot.setView(ViewRoot.java:513)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.Dialog.show(Dialog.java:241)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ProgressDialog.show(ProgressDialog.java:107)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ProgressDialog.show(ProgressDialog.java:90)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在Firstdroid.Gps.MapViewer.initMyLocation(MapViewer.java:64)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在Firstdroid.Gps.MapViewer.onCreate(MapViewer.java:37)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
6月8日至4日:29:23.804:ERROR / AndroidRuntime(8896):11 ...更多
6月8日至4日:29:23.814:WARN / ActivityManager(96):强制完成活动Firstdroid.Gps / .MapViewer
解决方案
在传递到上下文中的 ProgressDialog
必须是活动
,而不是应用
。
修改
对话框= ProgressDialog.show(getApplicationContext(),请稍候...,检索GPS数据......,真);
到
对话框= ProgressDialog.show(这一点,请稍候...,检索GPS数据......,真);
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
解决方案
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);
这篇关于等待GPS定位对话框抛出坏令牌例外 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文