最近我的Android项目已从Google地图切换到ESRI ArcGIS,目前我正处于转型过程中。这是我自那以后面临的一个小问题。
每当启用内置位置服务时,以下代码都会抛出NullPointerException
。并且无论LocationService.start()调用是在try-catch块中还是在另一个线程中运行都没关系:应用程序是否崩溃。
以下是Activity
类的摘录:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_esri);
map = (MapView) findViewById(R.id.map);
map.setExtent(boink);
map.setOnStatusChangedListener(new OnStatusChangedListener() {
private static final long serialVersionUID = 1L;
@Override
public void onStatusChanged(View source, STATUS status) {
if(status == STATUS.INITIALIZED) {
//set up location service
/*
* The following block throws exception and the application crashes.
* And it doesn't matter whether I run it from another thread or in a try-catch block!
*/
new Handler().post(new Runnable() {
@Override
public void run() {
try {
locationService = map.getLocationService();
locationService.setLocationListener(MapEsri.this);
// locationService.setAccuracyCircleOn(true);
// locationService.setBearing(true);
// locationService.setAutoPan(true);
locationService.start();
}
catch(Exception e) {
e.printStackTrace();
}
}
});
}
}
});
}
这是它抛出的例外:
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): java.lang.NullPointerException
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.esri.core.geometry.Envelope.<init>(Unknown Source)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.esri.core.map.LayerModel.setExtent(Unknown Source)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.esri.android.map.LayerView.onSizeChanged(Unknown Source)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.View.setFrame(View.java:7123)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.View.layout(View.java:7050)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.esri.android.map.MapView.onLayout(Unknown Source)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.View.layout(View.java:7056)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.View.layout(View.java:7056)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.View.layout(View.java:7056)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.os.Looper.loop(Looper.java:143)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at android.app.ActivityThread.main(ActivityThread.java:5068)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 10:08:55.580: ERROR/AndroidRuntime(28355): at dalvik.system.NativeStart.main(Native Method)
我做错了什么?
答案 0 :(得分:0)
这可能有点晚了但状态可能来自另一个来源。 因此,当初始化其他内容时,映射就会抛出null异常。
if(source == map && status == STATUS.INITIALIZED) {
//set up location service
/*
* The following block throws exception and the application crashes.
* And it doesn't matter whether I run it from another thread or in a try-catch block!
*/
new Handler().post(new Runnable() {
@Override
public void run() {
try {
locationService = map.getLocationService();
locationService.setLocationListener(MapEsri.this);
// locationService.setAccuracyCircleOn(true);
// locationService.setBearing(true);
// locationService.setAutoPan(true);
locationService.start();
}
catch(Exception e) {
e.printStackTrace();
}
}
});
}