单击地图视图中的覆盖项目时崩溃

发布于 2024-11-25 03:19:52 字数 8179 浏览 1 评论 0原文

    @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;
}

}

    @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);
}

When i click on the overlay item the application crashes. anybody know the reason why?

the logcat is a bit odd too as it appears to be trying to launch an activity but all i want it to do is show a box with the name when clicked.

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 here is the overlay class:

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;
}

}

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

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

发布评论

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

评论(2

欲拥i 2024-12-02 03:19:52

将此行

BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable);

替换为

 BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable, this);

您尚未传递上下文,因此膨胀 AlertDialog 会导致失败。

Replace this line

BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable);

with

 BrMapViewItemOverlay itemizedoverlay = new BrMapViewItemOverlay(drawable, this);

You have not passed the context and hence inflating AlertDialog is causing failure.

眼前雾蒙蒙 2024-12-02 03:19:52

从 logcat 输出中,我可以说您的 Tabs.java 文件中的行号 192 存在问题,在同一行中,我确信您正在尝试引用任何资源,如字符串、颜色、可绘制对象或任何资源,但它给出 ResourceNotFoundException 只是因为它实际上没有获取该资源的 ID。

只是为了尝试,执行 project -> clean 以便再次构建项目。这只是我的猜测,因为我也做过很多次。

简而言之,只需检查 Tabs.java 文件中的第 192 行即可。

From the logcat output, i can say there is a problem at line number 192 in your Tabs.java file, in the same line, i am sure you are trying to refer any resources like strings, colors, drawables or any but it is giving ResourceNotFoundException just because of it is not getting the ID of that resource actually.

Just for try, do the project -> clean so that it builds the project again. Its just my guess as i had also did many times.

In short, just check the line number 192 in Tabs.java file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文