我正在使用来自 github 的 mapviewballoons,它正在工作到我试图从 ballonItemizedOverlay 打开另一个活动的地步
private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
private Context c;
public mlgwItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenter(defaultMarker), mapView);
c = mapView.getContext();
}
public void addOverlay(OverlayItem overlay) {
m_overlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return m_overlays.get(i);
}
@Override
public int size() {
return m_overlays.size();
}
@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
Intent Details = new Intent(c, BlogActivity.class);
//Details.putExtra("Id", 1327);
c.startActivity(Details);
return true;
}
}
这是我的主要活动
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
mlgwItemizedOverlay itemizedOverlay;
mlgwItemizedOverlay itemizedOverlay2;
private static final class LatLonPoints extends GeoPoint {
public LatLonPoints(double latitude, double longitude) {
super((int) (latitude * 1E6), (int) (longitude * 1E6));
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (TapControlledMapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// dismiss balloon upon single tap of MapView (iOS behavior)
mapView.setOnSingleTapListener(new OnSingleTapListener() {
@Override
public boolean onSingleTap(MotionEvent e) {
itemizedOverlay.hideAllBalloons();
return true;
}
});
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new mlgwItemizedOverlay(drawable, mapView);
// set iOS behavior attributes for overlay
itemizedOverlay.setShowClose(false);
itemizedOverlay.setShowDisclosure(true);
itemizedOverlay.setSnapToCenter(false);
GeoPoint StartPoint = new LatLonPoints(35.149534,-90.04898);
// first overlay
GeoPoint point = new LatLonPoints(35.139646,-90.05521);
OverlayItem overlayItem = new OverlayItem(point, "Hello", "I'm am the first location");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
GeoPoint point2 = new LatLonPoints(35.020922,-90.027045);
OverlayItem overlayItem2 = new OverlayItem(point2, "Hello", "I'm am the second location");
itemizedOverlay.addOverlay(overlayItem2);
mapOverlays.add(itemizedOverlay);
GeoPoint point3 = new LatLonPoints(35.151009,-89.978281);
OverlayItem overlayItem3 = new OverlayItem(point3, "Hello", "I'm am the third location");
itemizedOverlay.addOverlay(overlayItem3);
mapOverlays.add(itemizedOverlay);
GeoPoint point4 = new LatLonPoints(35.088971,-89.965608);
OverlayItem overlayItem4 = new OverlayItem(point4, "Hello", "I'm am the fourth location");
itemizedOverlay.addOverlay(overlayItem4);
mapOverlays.add(itemizedOverlay);
GeoPoint point5 = new LatLonPoints(35.341282,-89.8922298);
OverlayItem overlayItem5 = new OverlayItem(point5, "Hello", "I'm am the fifth location");
itemizedOverlay.addOverlay(overlayItem5);
mapOverlays.add(itemizedOverlay);
if (savedInstanceState == null) {
final MapController mc = mapView.getController();
mc.animateTo(StartPoint);
mc.setZoom(11);
} else {
// example restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0) {
itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
}
focused = savedInstanceState.getInt("focused_2", -1);
if (focused >= 0) {
itemizedOverlay2.setFocus(itemizedOverlay2.getItem(focused));
}
}
}
public void startCustomActivity(){
Intent Details = new Intent(getBaseContext(), BlogActivity.class);
Details.putExtra("Id", 1327);
startActivity(Details);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// example saving focused state of overlays
if (itemizedOverlay.getFocus() != null) outState.putInt("focused_1", itemizedOverlay.getLastFocusedIndex());
if (itemizedOverlay2.getFocus() != null) outState.putInt("focused_2", itemizedOverlay2.getLastFocusedIndex());
super.onSaveInstanceState(outState);
}
}
这是我试图打开的博客活动
public class BlogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Blog tab");
setContentView(textview);
}
}
一旦我按下覆盖,应用程序就会崩溃,并且返回的错误是:
04-18 04:39:56.465: E/MapActivity(8938): Couldn't get connection factory client
04-18 04:40:03.075: E/AndroidRuntime(8938): FATAL EXCEPTION: main
04-18 04:40:03.075: E/AndroidRuntime(8938): java.lang.RuntimeException: Unable to pause activity {com.mlgw.MlgwMapView/com.mlgw.MlgwMapView.MlgwMapViewActivity}: java.lang.NullPointerException
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.os.Looper.loop(Looper.java:130)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-18 04:40:03.075: E/AndroidRuntime(8938): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 04:40:03.075: E/AndroidRuntime(8938): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 04:40:03.075: E/AndroidRuntime(8938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-18 04:40:03.075: E/AndroidRuntime(8938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-18 04:40:03.075: E/AndroidRuntime(8938): at dalvik.system.NativeStart.main(Native Method)
04-18 04:40:03.075: E/AndroidRuntime(8938): Caused by: java.lang.NullPointerException
04-18 04:40:03.075: E/AndroidRuntime(8938): at com.mlgw.MlgwMapView.MlgwMapViewActivity.onSaveInstanceState(MlgwMapViewActivity.java:131)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.Activity.performSaveInstanceState(Activity.java:1037)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)
04-18 04:40:03.075: E/AndroidRuntime(8938): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2336)
04-18 04:40:03.075: E/AndroidRuntime(8938): ... 12 more
我的主要目标是将更多关于他们单击的注释的数据传递给新活动。
你能告诉我我做错了什么或指出我正确的解决方案。我在谷歌上搜索了其他答案,但没有找到任何有用的答案。