如何在android中使用serializable将arraylist传递给intent

时间:2016-01-19 05:38:47

标签: android android-intent

我正在将arraylist传递给在android中使用serializable的意图。我的Product类实现了serializable。它抛出一个空指针异常。如何解决这个问题。请帮助我。

第一个活动代码:

ArrayList<Product> data = new ArrayList<Product>();
Intent intent = new Intent(this.getActivity(),Viewcart.class);
Bundle args = new Bundle();
args.putSerializable("LIST",(Serializable)data);
intent.putExtras(args);
startActivity(intent);

第二个活动代码:

Intent intent = getIntent();
ArrayList<Product> data=(ArrayList<Product>)intent.getSerializableExtra("LIST");

产品类:

public class Product implements Serializable {

    private int productId;
    private int productPrice;
    private int productQty;
    private int productSalePrice;
    private String productName;
    private String productImage;
    private String productUrl;
    private int userQty;
    private int productValue;
    private int productTotalUserPrice;


    public int getUserQty() {
        return userQty;
    }

    public void setUserQty(int userQty) {
        this.userQty = userQty;
    }
    public int getProductValue() {
        return productValue;
    }

    public void setProductValue(int productValue) {
        this.productValue = productValue;
    }
    public int getTotalproductSalePrice() {
        return userQty;
    }

    public void setTotalproductSalePrice(int totalproductSalePrice) {
        this.userQty = totalproductSalePrice;
    }

    public int getProductTotalUserPrice() {
        return productTotalUserPrice;
    }

    public void setProductTotalUserPrice(int productTotalUserPrice) {
        this.productTotalUserPrice = productTotalUserPrice;
    }

    public Product() {
    }

    public Product(int productId, int productPrice, int productQty, int productSalePrice, String productName, String productImage, String productUrl) {
        this.productId = productId;
        this.productPrice = productPrice;
        this.productQty = productQty;
        this.productSalePrice = productSalePrice;
        this.productName = productName;
        this.productImage = productImage;
        this.productUrl = productUrl;
    }
}

错误日志cat:

PID: 26286                                                           

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.sureshkirana/com.app.sureshkirana.Viewcart}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2214)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5234)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
at com.app.sureshkirana.Viewcart.<init>(Viewcart.java:34)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 
at android.app.ActivityThread.access$800(ActivityThread.java:147) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5234) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

2 个答案:

答案 0 :(得分:1)

只需在MainActivity.java

中将代码更改为此代码即可
List<Product> data = new ArrayList<>();
Intent intent = new Intent(this.getActivity(),Viewcart.class);
intent.putExtra("LIST", data);
startActivity(intent);

SecondActivity,即Viewcart.java

Intent intent = getIntent();
List<Product> = new ArrayList<>();
data=(List<Product>)intent.getSerializableExtra("LIST");

答案 1 :(得分:-1)

我建议使用Android应用程序类

Public MainApplication extend Application {

    public Arraylist yourlist;

}