我正在尝试在两个活动之间传递一个订单对象。此对象包含 MenuItems 的 ArrayList。Order 的类如下:(它实现了 parcelable 接口)。
public class Order extends ArrayList <MenuItem> implements Parcelable {
private int covers;
private int table;
//To hold MenuItems
private ArrayList<MenuItem> items;
public Order(){
}
public static final Parcelable.Creator<Order> CREATOR = new Parcelable.Creator<Order>() {
@Override
public Order createFromParcel(Parcel source) {
return new Order(source);
}
@Override
public Order[] newArray(int size) {
return new Order[size];
}
};
public Order(int covers, int table, ArrayList<MenuItem> items) {
super();
this.covers = covers;
this.table = table;
this.items = items;
}
public int getCovers() {
return covers;
}
public void setCovers(int covers) {
this.covers = covers;
}
public int getTable() {
return table;
}
public void setTable(int table) {
this.table = table;
}
public ArrayList<MenuItem> getItems() {
return items;
}
public void setItems(ArrayList<MenuItem> items) {
this.items = items;
}
//add menu item to order
public void addToOrder(MenuItem m){
items.add(m);
}
//remove menu item from order
public void removeFromOrder(MenuItem m){
items.remove(m);
}
public Order(Parcel source) {
readFromParcel(source);
}
private void readFromParcel(Parcel source) {
this.clear();
//read the size of the list
int size = source.readInt();
//Remember order of items written into the Parcel. Important here.
for(int i = 0; i < size; i ++){
MenuItem item = new MenuItem();
item.setName(source.readString());
item.setPrice(source.readDouble());
this.add(item);
}
}
@Override
public void writeToParcel(Parcel dest, int arg1) {
int size = this.size();
dest.writeInt(size);
for(int i = 0; i < size; i ++){
MenuItem item = this.get(i);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
当我的活动开始时,我创建一个新的订单项目并将其放入 Intent 以发送到下一个活动。当我单击按钮开始新活动时,我收到以下错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.waitron5/com.example.waitron5.SectionsActivity}:java.lang.NullPointerException
下面是我在 MainActivity.java 中用于将对象放入 Intent 的代码。
//Order object to hold details about order for duration of session
Order order;
ArrayList<Order> orderList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//As soon as app is started up create an order object.
order = new Order();
orderList = new ArrayList<Order>();
orderList.add(order);
//reference to button and add listeners
orderBtn = (Button)findViewById(R.id.orderBtn);
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Create a new intent and start up the sections activity...
i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", (ArrayList<Order>)orderList);
startActivity(i);
}
});
}
下面是我在 SectionsActivity.java 中使用的代码,用于从 Intent 中检索我的对象。
//Get order information
public void getOrderData(){
i = getIntent();
if(i != null && i.hasExtra("data")){
Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_SHORT).show();
order = i.getParcelableExtra("data");
int size = order.size();
Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_SHORT).show();
}
}
有谁知道我为什么会收到这个错误。我知道这是因为它找不到可包裹的对象,我只是不知道我应该如何修复它!非常感谢。
编辑:我认为问题出在这段代码中:
@Override
public void writeToParcel(Parcel dest, int flags) {
int size = this.size();
dest.writeInt(size);
for(int i = 0; i < size; i ++){
MenuItem item = this.get(i);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
记录 CAT 错误
01-09 23:41:23.706: E/AndroidRuntime(22717): 致命异常: main 01-09 23:41:23.706: E/AndroidRuntime(22717): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.waitron5/com.example.waitron5.SectionsActivity}:java.lang.NullPointerException 01-09 23:41:23.706:E/AndroidRuntime(22717):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01 -09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app .ActivityThread.access$600(ActivityThread.java:141) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-09 23: 41:23.706: E/AndroidRuntime(22717): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.os.Looper.loop(Looper.java:137) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在android.app.ActivityThread.main(ActivityThread.java:5039) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-09 23:41 :23.706: E/AndroidRuntime(22717): at java.lang.reflect.Method.invoke(Method.java:511) 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01 -09 23:41:23.706: E/AndroidRuntime(22717): at dalvik.system.NativeStart.main(Native Method) 01-09 23:41:23.706: E/AndroidRuntime(22717): Caused by: java.lang. NullPointerException 01-09 23:41:23.706:E/AndroidRuntime(22717): 在 com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity.java:113) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 com.example.waitron5.SectionsActivity.onCreate (SectionsActivity.java:44) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.Activity.performCreate(Activity.java:5104) 01-09 23:41:23.706: E/AndroidRuntime (22717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-09 23:41:23.706: E/AndroidRuntime(22717): ... 11 更多onCreate(SectionsActivity.java:44) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.Activity.performCreate(Activity.java:5104) 01-09 23:41:23.706: E/ AndroidRuntime(22717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144 ) 01-09 23:41:23.706: E/AndroidRuntime(22717): ... 11 更多onCreate(SectionsActivity.java:44) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.Activity.performCreate(Activity.java:5104) 01-09 23:41:23.706: E/ AndroidRuntime(22717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-09 23:41:23.706: E/AndroidRuntime(22717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144 ) 01-09 23:41:23.706: E/AndroidRuntime(22717): ... 11 更多E/AndroidRuntime(22717): ... 11 更多E/AndroidRuntime(22717): ... 11 更多
记录猫警告
* 01-09 23:41:23.676:W/Bundle(22717):关键数据预期 Parcelable,但值是 java.util.ArrayList。已返回默认值。 01-09 23:41:23.686: W/Bundle(22717): 尝试转换生成的内部异常: 01-09 23:41:23.686: W/Bundle(22717): java.lang.ClassCastException: java.util.ArrayList无法转换为 android.os.Parcelable 01-09 23:41:23.686: W/Bundle(22717): at android.os.Bundle.getParcelable(Bundle.java:1171) 01-09 23:41:23.686: W /Bundle(22717): 在 android.content.Intent.getParcelableExtra(Intent.java:4330) 01-09 23:41:23.686: W/Bundle(22717): 在 com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity. java:112) 01-09 23:41:23.686: W/Bundle(22717): 在 com.example.waitron5.SectionsActivity.onCreate(SectionsActivity.java:44) *