开发者

Mapview start new activity when a baloonOverlay is clicked

开发者 https://www.devze.com 2023-02-12 05:11 出处:网络
Hi I have implemented a google map with projection points so when i click a point it opens up a balloon showing some information relating to it. I have used thisgithub resource which makes it quite ea

Hi I have implemented a google map with projection points so when i click a point it opens up a balloon showing some information relating to it. I have used this github resource which makes it quite easier (thanks to Paul Sasik for guidance). What i want is when i click on each baloon it should open up a new Activity, I have tried implementing that in its onTap method but the problem is that i cant startActivity in a class that is extended from a framelayout class. So here is my code :

public class CouponMapView extends MapActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
               ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable, mapView);
               //all the mapoverlay code goes here...........
               //this part works fine.

            }
    }

//----Class ItemizedOverlay in which im calling startActivity-----

public class ItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
    private Context context;
    private int id;

    public ItemizedOverlay(Drawable defaultMarker, MapView mapView) {
        super(boundCenter(defaultMarker), mapView);
        context = mapView.getContext();
    }

    public void addOverlay(OverlayItem overlay) {
        overlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return overlays.get(i);
    }

    @Override
    public int size() {
        return overlays.size();
    }

    @Override
    protected boolean onBalloonTap(int index) {
//---I cant call startActivity here as this class is not inhereted from Activity its //extended from a class that is extended from FrameLayout that you will find if you scroll down.
        SubActivity sub = new SubActivity();
        sub.startCustomActivity();
        return true;
    }
}

//--To start a new activity and to call startActivity method i have to create a new class and extends it from Activity...

public class SubActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentVi开发者_Python百科ew(R.layout.main);
    }
    public void startCustomActivity(){
        Intent Details = new Intent(getBaseContext(), Tab.class);
        Details.putExtra("Id", 1327);
        startActivity(Details);
    }

}

Output: cant paste image directly the link is this mapView show overlay details when clicked

The image above is similar to what i am getting so when i click on the overlay this should startup a new intent activity where i want to show some details.

But when i click on it i get these errors. I am not sure if i am doing it the right way. I am calling the subActivity class when the image is clicked.

02-21 19:21:44.769: ERROR/AndroidRuntime(462): FATAL EXCEPTION: main
02-21 19:21:44.769: ERROR/AndroidRuntime(462): java.lang.NullPointerException
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.content.ComponentName.<init>(ComponentName.java:75)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.content.Intent.<init>(Intent.java:2678)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at com.vouchacha.mvc.mapview.SubActivity.startCustomActivity(SubActivity.java:17)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at com.vouchacha.mvc.mapview.ItemizedOverlay.onBalloonTap(ItemizedOverlay.java:52)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at com.vouchacha.mvc.mapview.BalloonItemizedOverlay$1.onTouch(BalloonItemizedOverlay.java:177)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.View.dispatchTouchEvent(View.java:3762)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-21 19:21:44.769: ERROR/AndroidRuntime(462):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)


very simple resolution for this:

replace this:

SubActivity sub = new SubActivity();
sub.startCustomActivity();

with this:

Intent Details = new Intent(context, Tab.class);
Details.putExtra("Id", 1327);
context.startActivity(Details);

use the context variable you got from the MapView you passed in your overlay class to start your activity.


Here is a full implementation example using onBallonTap to start a new Activity (using startActivity and startActivityForResult).

public class StoreItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

  private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  private Context mContext;
  private Activity mActivity;
  private StoreGroup mGroup;
  private boolean mCheckIn;

  public StoreItemizedOverlay(Drawable defaultMarker, MapView mapView, 
        Activity activity, StoreGroup group, boolean checkIn) {
    super(boundCenterBottom(defaultMarker), mapView);
    mContext = mapView.getContext();
    mGroup = group;
    mCheckIn = checkIn;
    mActivity = activity;
  }

  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 onBalloonTap(int index) {
    try {
        Store store = mGroup.getStore(index);
        Intent intent = new Intent(mContext, StoreActivity.class);
        intent.putExtra("store", store.toJSONObject().toString());
        if (mCheckIn) {
            intent.putExtra("checkin", mCheckIn);
            mActivity.startActivityForResult(intent, 0);
        } else
            mContext.startActivity(intent);
    } catch (JSONException e) {
        Log.d(TAG, "Store JSON encoding error", e);
    }
    return true;
  }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消