您好我正在处理一个应用程序,并尝试将对象列表(IntentBinders)从MainActivity传递给RouteSummary类,但应用程序崩溃时出现异常。
public class IntentBinder implements Serializable {
public String rtName;
public double rtDistance;
public int rtfare;
}
MainActivity:
Intent passIntent = new Intent(getApplicationContext(),RouteSummary.class);
IntentBinder obj1 = new IntentBinder();
obj1.rtName = finalRt1Name;
obj1.rtDistance = distanceRt1;
obj1.rtfare = fareRt1;
IntentBinder obj2 = new IntentBinder();
obj2.rtName = finalRt2Name;
obj2.rtDistance = distanceRt2;
obj2.rtfare = fareRt2;
ArrayList<IntentBinder> list = new ArrayList<IntentBinder>();
list.add(obj1);
list.add(obj2);
Bundle bundleObj = new Bundle();
bundleObj.putSerializable("key", list);
passIntent.putExtras(bundleObj);
startActivity(passIntent);
RouteSummary.Java(接收活动)
setContentView(R.layout.route_summary);
try{
// Get thIne Bundle Object
Bundle bundleObject = getIntent().getExtras();
// Get ArrayList Bundle
ArrayList<IntentBinder> classObject = (ArrayList<IntentBinder>) bundleObject.getSerializable("key");
for(int index = 0; index < classObject.size(); index++){
IntentBinder Object = classObject.get(index);
Toast.makeText(getApplicationContext(), "Id is :"+Object.rtName, Toast.LENGTH_SHORT).show();
}
} catch(Exception e){
e.printStackTrace();
}
LogCat:
05-27 14:36:50.697: E/AndroidRuntime(31484): FATAL EXCEPTION: main
05-27 14:36:50.697: E/AndroidRuntime(31484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webmonopolists.project_linx/com.webmonopolists.project_linx.RouteSummary}: java.lang.NullPointerException
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1734)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1753)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread.access$1500(ActivityThread.java:155)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.os.Handler.dispatchMessage(Handler.java:130)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.os.Looper.loop(SourceFile:351)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread.main(ActivityThread.java:3820)
05-27 14:36:50.697: E/AndroidRuntime(31484): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 14:36:50.697: E/AndroidRuntime(31484): at java.lang.reflect.Method.invoke(Method.java:538)
05-27 14:36:50.697: E/AndroidRuntime(31484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
05-27 14:36:50.697: E/AndroidRuntime(31484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
05-27 14:36:50.697: E/AndroidRuntime(31484): at dalvik.system.NativeStart.main(Native Method)
05-27 14:36:50.697: E/AndroidRuntime(31484): Caused by: java.lang.NullPointerException
05-27 14:36:50.697: E/AndroidRuntime(31484): at com.webmonopolists.project_linx.RouteSummary.onCreate(RouteSummary.java:42)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-27 14:36:50.697: E/AndroidRuntime(31484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
05-27 14:36:50.697: E/AndroidRuntime(31484): ... 11 more
答案 0 :(得分:0)
改为使用public class IntentBinder implements Serializable
工具Parcelable
。
要ArrayList
使用IntentBinder
:
i.putParcelableArrayListExtra(name, value)
获取使用:
getIntent().getParcelableArrayListExtra(name)