制作一个简单的地图应用,计划添加按钮以标记特定位置
但是当我缩放时它会崩溃。
import java.util.List;
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;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class Main extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
当我触摸内置缩放时,会强制应用程序关闭。
任何帮助都会很棒。
编辑:Stacktrace
12-16 05:40:04.012: D/dalvikvm(2473): GC_FOR_MALLOC freed 4578 objects / 277208 bytes in 49ms
12-16 05:40:04.151: I/MapActivity(2473): Handling network change notification:CONNECTED
12-16 05:40:04.151: E/MapActivity(2473): Couldn't get connection factory client
12-16 05:40:04.271: D/dalvikvm(2473): GC_FOR_MALLOC freed 3049 objects / 271640 bytes in 49ms
12-16 05:40:08.491: D/AndroidRuntime(2473): Shutting down VM
12-16 05:40:08.491: W/dalvikvm(2473): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-16 05:40:08.551: E/AndroidRuntime(2473): FATAL EXCEPTION: main
12-16 05:40:08.551: E/AndroidRuntime(2473): java.lang.NullPointerException
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.b00517566.la.MyItemizedOverlay.onTap(MyItemizedOverlay.java:46)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.View.dispatchTouchEvent(View.java:3766)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.os.Looper.loop(Looper.java:123)
12-16 05:40:08.551: E/AndroidRuntime(2473): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 05:40:08.551: E/AndroidRuntime(2473): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 05:40:08.551: E/AndroidRuntime(2473): at java.lang.reflect.Method.invoke(Method.java:521)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 05:40:08.551: E/AndroidRuntime(2473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 05:40:08.551: E/AndroidRuntime(2473): at dalvik.system.NativeStart.main(Native Method)
12-16 05:40:11.023: I/Process(2473): Sending signal. PID: 2473 SIG: 9
修改
package com.bleh.la;
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 MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
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();
}
public MyItemizedOverlay(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;
}
}
答案 0 :(得分:1)
您的MyItemizedOverlay.mContext为null,这会导致NPE。我建议你删除只接受drawable的构造函数,并将活动中overlay的创建更改为
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
构造函数还有另一个区别 - 在第二个中忽略对boundCenterBottom的调用 - 这是故意的吗?:
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
答案 1 :(得分:1)
我认为,这一行会给你空指针异常
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
此处 mContext为空
因为在你正在呼叫的Main.java
,
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable);
而不是打电话
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable,this);
你的构造函数应该是,
public MyItemizedOverlay(Drawable drawable,Context cxt){
super(boundCenter(drawable));
this.mContext = cxt;
}
编辑:查看Sample Project