单击baloonOverlay时,Mapview将启动新活动

时间:2011-02-21 20:40:28

标签: android

嗨我已经实现了一个带有投影点的谷歌地图,所以当我点击一个点时,它会打开一个气球,显示一些与之相关的信息。我使用了这个github资源,这使得它变得更加容易(感谢Paul Sasik的指导)。我想要的是当我点击每个气球它应该打开一个新的Activity,我已经尝试在其onTap方法中实现它,但问题是我不能在一个从framelayout类扩展的类中的startActivity。所以这是我的代码:

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.

            }
    }

// ----类ItemizedOverlay,其中我调用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;
    }
}

// - 要开始新活动并调用startActivity方法,我必须创建一个新类并将其从Activity扩展...

public class SubActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void startCustomActivity(){
        Intent Details = new Intent(getBaseContext(), Tab.class);
        Details.putExtra("Id", 1327);
        startActivity(Details);
    }

}

输出: 直接粘贴图像链接就是这个 mapView show overlay details when clicked

上面的图像与我得到的图像类似,所以当我点击叠加层时,这应该启动一个新的意图活动,我想要显示一些细节。

但是当我点击它时我得到了这些错误。我不确定我是否正确地做到了这一点。我在点击图像时调用了subActivity类。

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)

2 个答案:

答案 0 :(得分:3)

非常简单的解决方案:

替换这个:

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

用这个:

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

使用您在叠加层中传递的context获得的MapView变量来开始您的活动。

答案 1 :(得分:2)

以下是使用onBallonTap开始新Activity的完整实施示例(使用startActivitystartActivityForResult)。

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

}