gpt4 book ai didi

java - 在运行时加载自定义 ItemizedOverlay NullPointerException - Android

转载 作者:行者123 更新时间:2023-11-30 03:48:13 25 4
gpt4 key购买 nike

我正在为 android 制作一个 map 应用程序,它允许用户放置自己的兴趣点。我想为放置的每种类型的点设置自定义图钉图标。当我尝试在运行时获取我的自定义 ItemizedOverlay 时,我收到 NullPointerException。

在 map 上添加一个点:

public void addPointToMap(PointOfInterest pointOfInterest) {

OurItemizedOverlay itemizedOverlay = getOverlayForPointOfInterest(pointOfInterest); <--- NullPointer here
//Add item to collection in OurItemized Overlay
itemizedOverlay.addOverlay(pointOfInterest);
mapOverlays.clear();
mapOverlays.add(myLocationOverlay);
mapOverlays.add(itemizedOverlay);
mapView.invalidate();
}

获取叠加层:

private OurItemizedOverlay getOverlayForPointOfInterest(PointOfInterest pointOfInterest) {

String type = pointOfInterest.getType().toLowerCase();

if(type.equals("monument")) {
return monumentOverlay;
}
else if(type.equals("building")) {
return buildingOverlay;
}
else if(type.equals("atm")) {
return atmOverlay;
}
else if(type.equals("attraction")) {
return attractionOverlay;
}
else if(type.equals("pub")) {
return pubOverlay;
}
else if(type.equals("restaurant")) {
return restaurantOverlay;
}
else if(type.equals("shop")) {
return shopOverlay;
}
else if(type.equals("bridge")) {
return bridgeOverlay;
}
else if(type.equals("station")) {
return stationOverlay;
}
else if(type.equals("cafe")) {
return cafeOverlay;
}
else if(type.equals("hotel")) {
return hotelOverlay;
}
else {
return defaultOverlay;
}
}

设置可绘制对象和叠加层:

/**
* Set the overlay objects up
*/
private void setItemizedOverlays() {

//SET OVERLAY Items
monumentOverlay = new OurItemizedOverlay(monumentIcon, this);
buildingOverlay = new OurItemizedOverlay(buildingIcon, this);
shopOverlay = new OurItemizedOverlay(shopIcon, this);
attractionOverlay = new OurItemizedOverlay(attractionIcon, this);
bridgeOverlay = new OurItemizedOverlay(bridgeIcon, this);
stationOverlay = new OurItemizedOverlay(stationIcon, this);
pubOverlay = new OurItemizedOverlay(pubIcon, this);
restaurantOverlay = new OurItemizedOverlay(restaurantIcon, this);
cafeOverlay = new OurItemizedOverlay(cafeIcon, this);
atmOverlay = new OurItemizedOverlay(atmIcon, this);
hotelOverlay = new OurItemizedOverlay(hotelIcon, this);


}

/**
* Set the icons for each Drawable object
*/
private void setDrawableIcons() {

defaultIcon = this.getResources().getDrawable(R.drawable.downarrow);
monumentIcon = this.getResources().getDrawable(R.drawable.monument);
shopIcon = this.getResources().getDrawable(R.drawable.shop);
attractionIcon = this.getResources().getDrawable(R.drawable.attraction);
buildingIcon = this.getResources().getDrawable(R.drawable.building);
bridgeIcon = this.getResources().getDrawable(R.drawable.bridge);
stationIcon = this.getResources().getDrawable(R.drawable.station);
pubIcon = this.getResources().getDrawable(R.drawable.pub);
restaurantIcon = this.getResources().getDrawable(R.drawable.restaurant);
cafeIcon = this.getResources().getDrawable(R.drawable.cafe);
atmIcon = this.getResources().getDrawable(R.drawable.atm);
hotelIcon = this.getResources().getDrawable(R.drawable.hotel);


}

堆栈跟踪:

01-29 18:17:11.875: E/AndroidRuntime(12380): FATAL EXCEPTION: main
01-29 18:17:11.875: E/AndroidRuntime(12380): java.lang.NullPointerException
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.example.mapproject.MainActivity.addPointToMap(MainActivity.java:140)
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.example.mapproject.MainActivity.onPointsOfInterestDownloaded(MainActivity.java:227)
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:67)
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.example.mapproject.DownloadPointsOfInterest.onPostExecute(DownloadPointsOfInterest.java:1)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.os.AsyncTask.finish(AsyncTask.java:602)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.os.Looper.loop(Looper.java:137)
01-29 18:17:11.875: E/AndroidRuntime(12380): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-29 18:17:11.875: E/AndroidRuntime(12380): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 18:17:11.875: E/AndroidRuntime(12380): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-29 18:17:11.875: E/AndroidRuntime(12380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-29 18:17:11.875: E/AndroidRuntime(12380): at dalvik.system.NativeStart.main(Native Method)

任何指针将不胜感激!

最佳答案

最有可能的错误 - 你进入了默认情况,我没有在 setItemizedOverlays 中看到 defaultOverlay 的初始化。但这对于调试器来说是一个很好的例子,弄清楚它在 getOverlayForPointOfInterest 中进入了哪个分支,并查看为什么它会变为 null。

关于java - 在运行时加载自定义 ItemizedOverlay NullPointerException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com