我是一位经验丰富的Android程序员。
我有一个列表活动,每个列表项都会打开一个新活动。应用程序中有一个地图,其中显示了位置的气球覆盖。有4个位置,当我点击任何气球覆盖时,我需要相应索引的活动。 例如:如果我点击index = 1的气球,我需要在活动中显示列表的第一项。
我目前正在使用以下代码。
@Override
protected boolean onBalloonTap(int index, OverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index,
Toast.LENGTH_LONG).show();
int index1 = index;
Intent i = new Intent(c,FestivalEvents.class);
i.putExtra("banner", "index1");
c.startActivity(i);
return true;
}
我正在捕捉另一个活动的意图,即如下的FestivalEvents.class
Bundle eventExtra = getIntent().getExtras();
if(eventExtra!=null){
int position = eventExtra.getInt("banner");
Log.i("banner", "" + position);
switch(position){
case 0: Toast.makeText(mContext, ""+ position, Toast.LENGTH_LONG).show();setUpDesc(position);break;
case 1:Toast.makeText(mContext, ""+ position,Toast.LENGTH_LONG).show();setUpDesc(position);break;
case 2:Toast.makeText(mContext, ""+ position, Toast.LENGTH_LONG).show();setUpDesc(position);break;
case 3:Toast.makeText(mContext, ""+ position, Toast.LENGTH_LONG).show();setUpDesc(position);break;
}
}
使用此代码,应用程序崩溃并出现空指针异常。我希望我的代码适合接收意图。或者在处理接收意图时我可能是错的。 我很有时间解决这个问题。请问有谁可以告诉我哪里可能出错了?
修改 我的logcat:
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): FATAL EXCEPTION: main
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sysponent.bedford/com.sysponent.bedford.FestivalEvents}: java.lang.NullPointerException
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.os.Looper.loop(Looper.java:130)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at java.lang.reflect.Method.invoke(Method.java:507)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168):at dalvik.system.NativeStart.main(Native Method)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): Caused by: java.lang.NullPointerException
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at com.sysponent.bedford.FestivalEvents.onCreate(FestivalEvents.java:239)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-02 18:45:38.858: ERROR/AndroidRuntime(21168):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
谢谢
答案 0 :(得分:0)
i.putExtra("banner", "index1");
不应该是
i.putExtra("banner", index1);