@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
badplatsName = getIntent().getStringExtra("se.brickit.badplatsen.badplatsname");
lat = getIntent().getIntExtra("se.brickit.badplatsen.lat",0);
lon = getIntent().getIntExtra("se.brickit.badplatsen.lon",0);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.smiley_flask);
BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable);
GeoPoint point = new GeoPoint(lat,lon);
OverlayItem overlayitem = new OverlayItem(point, badplatsName, "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapView.getController().animateTo(point);
mapView.getController().zoomToSpan(6, 6);
mapView.setSatellite(true);
}
当我点击叠加项时,应用程序崩溃。谁知道原因?
logcat也有点奇怪,因为它似乎试图启动一个活动,但我想要它做的就是在点击时显示一个带有名称的框。
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): FATAL EXCEPTION: main
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): java.lang.NullPointerException
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at se.brickit.badplatsen.BrMapViewItemOverlay.onTap(BrMapViewItemOverlay.java:43)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.View.dispatchTouchEvent(View.java:3765)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.app.Activity.dispatchTouchEvent(Activity.java:2093)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.os.Looper.loop(Looper.java:144)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at android.app.ActivityThread.main(ActivityThread.java:4937)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 14:36:56.303: ERROR/AndroidRuntime(15856): at dalvik.system.NativeStart.main(Native Method)
07-20 14:36:58.163: ERROR/ActivityManager(96): fail to set top app changed!
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): FATAL EXCEPTION: main
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.brickit.badplatsen/se.brickit.badplatsen.Tabs}: android.content.res.Resources$NotFoundException: Resource ID #0x0
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.os.Looper.loop(Looper.java:144)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread.main(ActivityThread.java:4937)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at dalvik.system.NativeStart.main(Native Method)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.content.res.Resources.getValue(Resources.java:892)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.content.res.Resources.getDrawable(Resources.java:580)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at se.brickit.badplatsen.Tabs.onCreate(Tabs.java:192)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
07-20 14:36:58.243: ERROR/AndroidRuntime(15896): ... 11 more
07-20 14:37:00.053: ERROR/ClockWidget(209): weatherClock onReceive~ mUseAnimation:false
07-20 14:37:00.053: ERROR/ClockWidget(209): weatherClock onReceive~ mUseAnimation:false
这里的Ps是叠加类:
public class BrMapViewItemOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public BrMapViewItemOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public BrMapViewItemOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
答案 0 :(得分:1)
替换此行
BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable);
带
BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable, this);
您尚未传递上下文,因此导致AlertDialog导致失败。
答案 1 :(得分:0)
从logcat输出,我可以说 Tabs.java 文件中的行号 192 存在问题,在同一行,我相信你是尝试引用任何资源,如字符串,颜色,drawables或任何资源,但它只是因为它没有实际获得该资源的ID而给出ResourceNotFoundException。
只是尝试一下,执行 project -> clean
,以便再次构建项目。这只是我的猜测,因为我也做了很多次。
简而言之,只需检查Tabs.java文件中的第192行。