我正在构建一个谷歌地图应用程序,标记地图上的几个点。我正在尝试在单击标记时显示对话框。现在,标记显示在正确的位置,但是当我单击它们时,整个应用程序意外停止/强制关闭。有没有人对错误以及如何修复它有任何建议?
MyMapsActivity.java类:
package net.learn2develop.MyGoogleMapsNew;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MyMapsActivity extends MapActivity {
/** Called when the activity is first created. */
public MyItemOverlay itemoverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
//MapView = new MapView(this, "example_Maps_ApiKey_String");
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
itemoverlay = new MyItemOverlay(drawable);
GeoPoint point1 = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem1 = new OverlayItem(point1, "Hola, Mundo!", "I'm in Mexico City!");
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
GeoPoint point3 = new GeoPoint(39000000, -77000000);
OverlayItem overlayitem3 = new OverlayItem(point3, "","");
itemoverlay.addOverlay(overlayitem1);
itemoverlay.addOverlay(overlayitem2);
itemoverlay.addOverlay(overlayitem3);
mapOverlays.add(itemoverlay);
}
protected boolean isRouteDisplayed() {
return false;
}
}
MyItemOverlay类:
package net.learn2develop.MyGoogleMapsNew;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MyItemOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public MyItemOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
@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;
}
/* 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.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
return true;
}
}*/
// @Override
/*protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return null;
}*/
/*@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}*/
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
}
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.MyGoogleMapsNew"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MyMapsActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.ACCESS_COARSE_LOCATION">
</uses-permission>
<uses-sdk android:minSdkVersion="3" />
</manifest>
答案 0 :(得分:4)
更改
itemoverlay = new MyItemOverlay(drawable);
到
itemoverlay = new MyItemOverlay(drawable, this);
MyMapsActivity.java
中的
答案 1 :(得分:0)
在MyMapsActivity.java中,您需要更改:
itemoverlay = new MyItemOverlay(drawable);
(如Bobby P所述)
itemoverlay = new MyItemOverlay(drawable, this);
从:
更改MyItemOverlay.java中的第二个构造函数public MyItemOverlay(Drawable defaultMarker, Context context)
{
super(defaultMarker);
mContext = context;
}
到:
public MyItemOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}