gpt4 book ai didi

android - Google Maps MapView 在 Android 中点击时崩溃?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:12 25 4
gpt4 key购买 nike

我目前正在编写一个显示 MapView 的应用程序,该 MapView 显示谷歌地图。我使用了来自 http://developer.android.com/resources/tutorials/views/hello-mapview.html 的“你好 map View ”教程到目前为止,这是有效的: map 正确显示、居中和缩放,甚至我添加的 OverlayItems 也正确显示。

但是当我点击我添加的其中一个覆盖项时,应用程序崩溃并出现空指针异常:

07-23 16:24:48.167: ERROR/AndroidRuntime(2530): java.lang.NullPointerException
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:753)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at net.xenonite.wifiloc.act_map$MapItemizedOverlay.onTap(act_map.java:182)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.View.dispatchTouchEvent(View.java:3709)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.os.Looper.loop(Looper.java:123)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at android.app.ActivityThread.main(ActivityThread.java:4595)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at java.lang.reflect.Method.invoke(Method.java:521)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530): at dalvik.system.NativeStart.main(Native Method)

我的代码主要是从教程中复制过来的:

public class act_map extends MapActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);

intent = getIntent();
lat = intent.getDoubleExtra("lat", 0.0);
lng = intent.getDoubleExtra("lng", 0.0);

MapView = (MapView) findViewById(R.id.act_map_mapview);

List<Overlay> mapOverlays = MapView.getOverlays();

Drawable positionMarker
= getResources().getDrawable(R.drawable.act_map_androidmarker);
MapItemizedOverlay itemizedOverlay
= new MapItemizedOverlay(positionMarker);

geoPoint = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
Pointer = new OverlayItem(geoPoint, "Thats me!", "wohooo");

itemizedOverlay.addOverlay(Pointer);

mapOverlays.add(itemizedOverlay);

MapView.setSatellite(false);

MapController = MapView.getController();
MapController.setCenter(geoPoint);
MapController.setZoom(18);

MapView.setBuiltInZoomControls(true);
MapView.displayZoomControls(true);
}

@Override
protected boolean isRouteDisplayed()
{
return false;
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_I)
{
MapView.getController().setZoom(MapView.getZoomLevel() + 1);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_O)
{
MapView.getController().setZoom(MapView.getZoomLevel() - 1);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_S)
{
MapView.setSatellite(true);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_M)
{
MapView.setSatellite(false);
return true;
}

return false;
}

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public MapItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
}

public MapItemizedOverlay(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;
}
}

我该如何解决这个问题?

最佳答案

Android MapView 教程也有问题。当我点击图标时,屏幕会崩溃(会传递一个空的 mContext):

上述修复对我不起作用。相反,我更改了 MapActivity 类中的以下代码行:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

为此:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

您还需要通过添加缺少的 boundCenterBottom 来修复构造函数:

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}

(有漂亮的屏幕截图,但不能发布,因为我是新手:P)

希望能帮助其他仍然对本教程有疑问并修复他们的应用程序的人!

关于android - Google Maps MapView 在 Android 中点击时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3319158/

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