从MapView通过MainActivity启动Intent时是否为Null-Pointer异常?

时间:2011-09-19 16:18:43

标签: android maps nullpointerexception

我的应用中有一个MapView。单击overlayItem时,会弹出一个气泡,其中包含有关该位置的信息。单击该图标,用户应该转到另一个活动。

我首先从

中发送控件
BalloonItemizedOverlay<OverlayItem>.MapBalloonItemizedOverlay.onBalloonTap(int index, OverlayItem item)

使用以下方法访问MainActivity页面:

MainActivityObject.gotoMethod();

@Override
public boolean onBalloonTap(int index, OverlayItem item)
{
   MainActivityObject.gotoMethod();
    return true;
}

然后在那个方法中我编码:

try
    {
    Intent goIntent = new Intent(MainActivity.this, NextActivity.class);
    startActivity(goIntent);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

但我收到以下空指针异常错误:

09-19 16:02:56.707: WARN/System.err(2454): java.lang.NullPointerException
09-19 16:02:56.717: WARN/System.err(2454):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
09-19 16:02:56.717: WARN/System.err(2454):     at android.content.ComponentName.<init>(ComponentName.java:75)
09-19 16:02:56.717: WARN/System.err(2454):     at android.content.Intent.<init>(Intent.java:2678)
09-19 16:02:56.717: WARN/System.err(2454):     at towerCo.Android.Main.MapsActivity.showDetailpage(MapsActivity.java:897)
09-19 16:02:56.717: WARN/System.err(2454):     at towerCo.Android.MapOverLays.MapBalloonItemizedOverlay.onBalloonTap(MapBalloonItemizedOverlay.java:55)
09-19 16:02:56.717: WARN/System.err(2454):     at mapViewBalloon.BalloonItemizedOverlay$1.onTouch(BalloonItemizedOverlay.java:169)
09-19 16:02:56.717: WARN/System.err(2454):     at android.view.View.dispatchTouchEvent(View.java:3762)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.727: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
09-19 16:02:56.727: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-19 16:02:56.727: WARN/System.err(2454):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
09-19 16:02:56.738: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
09-19 16:02:56.738: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.738: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.738: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.738: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.738: WARN/System.err(2454):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
09-19 16:02:56.738: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
09-19 16:02:56.747: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-19 16:02:56.747: WARN/System.err(2454):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
09-19 16:02:56.747: WARN/System.err(2454):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
09-19 16:02:56.747: WARN/System.err(2454):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
09-19 16:02:56.747: WARN/System.err(2454):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 16:02:56.747: WARN/System.err(2454):     at android.os.Looper.loop(Looper.java:123)
09-19 16:02:56.747: WARN/System.err(2454):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-19 16:02:56.747: WARN/System.err(2454):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 16:02:56.747: WARN/System.err(2454):     at java.lang.reflect.Method.invoke(Method.java:521)
09-19 16:02:56.747: WARN/System.err(2454):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-19 16:02:56.747: WARN/System.err(2454):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-19 16:02:56.747: WARN/System.err(2454):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我想我已经在另一篇文章中找到了答案。我会在这里为其他人重新发布:

Mapview start new activity when a baloonOverlay is clicked

替换它:

MainActivity MainActivityObject = new MainActivity (); 
sub.startCustomActivity(); 

用这个:

Intent Details = new Intent(context, Tab.class); 
context.startActivity(Details); 

所以我们基本上必须使用我们从overlay类中传递的MapView获得的上下文变量来开始我们的活动。

感谢大家早些时候帮助我。理解!