简体   繁体   English

从onBalloonTap加载活动

[英]loading activity from onBalloonTap

I'm using the mapviewballoons from github and it is working up to the point where I'm trying to open another activity from a ballonItemizedOverlay 我正在使用来自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;
} 

} }

Here is my Main Activity 这是我的主要活动

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

}

} }

and here is blog activity that I trying to open 这是我尝试打开的博客活动

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

} }

Once I press the overlay the app crashes and the errors that are being returned are: 一旦按下叠加层,应用程序就会崩溃,并且返回的错误是:

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

My main goal is to pass more data about the annotation that they click on to the new activity. 我的主要目标是将有关他们单击的注释的更多数据传递给新活动。

Can you tell me what I'm doing wrong or point me to the right solution. 您能告诉我我做错了什么,还是指出正确的解决方案。 I've searched google for other answers but I haven't found any that work. 我已经在Google上搜索了其他答案,但没有找到任何可行的方法。

Your issue is that you are over-riding onSaveInstanceState but not calling the superclass method: do this. 您的问题是您要onSaveInstanceState但不调用超类方法:执行此操作。

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(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);

}

See if that helps. 看看是否有帮助。

try this : 尝试这个 :

protected boolean onBalloonTap(int index, OverlayItem item) { 受保护的boolean onBalloonTap(int index,OverlayItem item){
Intent Details = new Intent(c, BlogActivity.class); Intent Details = new Intent(c,BlogActivity.class);
//Details.putExtra("Id", 1327); //Details.putExtra("Id“,1327);
((Activity) c).startActivity(Details); ((活动)c).startActivity(详细信息);
return true; 返回true;
} }
  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);

        Bundle b=getIntent.getExtras();
        if(b!=null)
      {
       String s= b.getString("Id")
      }
        textview.setText("This is the Blog tab");
        setContentView(textview);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM