我正在尝试在两个活动之间传递Order对象。该对象包含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):致命异常:主要 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):at 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):引起:java.lang.NullPointerException 01-09 23:41:23.706:E / AndroidRuntime(22717):at com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity.java:113) 01-09 23:41:23.706:E / AndroidRuntime(22717):at 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更多
登录CAT警告
* 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):在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):at com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity.java:112) 01-09 23:41:23.686:W / Bundle(22717):at com.example.waitron5.SectionsActivity.onCreate(SectionsActivity.java:44) *
答案 0 :(得分:2)
查看完整的LogCat堆栈跟踪以了解发生错误的完全将非常有帮助,但我可以立即看到一个潜在的问题。
您的默认构造函数不会调用超级构造函数:
public Order(){
}
因此,当调用writeToParcel()
时,您尝试从可能为空列表的内容中获取值。
MenuItem item = this.get(i); // You never initialized the super class...
答案 1 :(得分:2)
我设法解决了我的问题。问题是Order类正在实现Parcelable。但是因为它包含一个MenuItems列表,所以还需要MenuItem类来实现Parcelable接口。
一旦我这样做,问题就解决了。
答案 2 :(得分:0)
更改i = getIntent();
通过
i = getIntent().getCharSequenceArrayListExtra("data");
更新以获取ArrayList