从 MapView 通过 MainActivity 启动 Intent 时出现空指针异常?
我的应用程序中有一个 MapView。单击覆盖项时,会弹出一个气泡,其中包含有关该地点的信息。单击该图标时,用户应该转到另一个活动。
我首先使用以下方法将控件从
BalloonItemizedOverlay<OverlayItem>.MapBalloonItemizedOverlay.onBalloonTap(int index, OverlayItem item)
方法内部发送到 MainActivity 页面:
MainActivityObject.gotoMethod();
ie
@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)
I have a MapView in my app. On clicking an overlayItem, a bubble pops-up with information about the place.On clicking the icon, the user should go to another activity.
I first send the control from inside the
BalloonItemizedOverlay<OverlayItem>.MapBalloonItemizedOverlay.onBalloonTap(int index, OverlayItem item)
method to the MainActivity page using:
MainActivityObject.gotoMethod();
i.e
@Override
public boolean onBalloonTap(int index, OverlayItem item)
{
MainActivityObject.gotoMethod();
return true;
}
Then in that method I code:
try
{
Intent goIntent = new Intent(MainActivity.this, NextActivity.class);
startActivity(goIntent);
}
catch(Exception e)
{
e.printStackTrace();
}
But I get the below null-pointer exception error:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我在另一篇文章中找到了答案。我将在这里为其他人重新发布:
Mapview 在单击 baloonOverlay 时启动新活动
将其替换
为:
因此,我们基本上必须使用从传入覆盖类的 MapView 中获取的上下文变量来启动我们的活动。
感谢大家早些时候对我的帮助。赞赏!
I think I have found the answer in another post.I will repost it here for others:
Mapview start new activity when a baloonOverlay is clicked
Replace this:
with this:
So we basically have to use the context variable we got from the MapView passed in our overlay class to start our activity.
Thank You to all for helping me earlier. Appreciated!