Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

Android Parcelable -- RetailerOrderActivity.java return null

I have to pass one activity to another activity:

I have SalesProduct enetity class:

 public class Product implements Parcelable{

    private double availableQuantity;
    private double price;
    private String productCode; 
    private String description; 
    private String description2;
    private String productGroup;
    private String alternateSearch;
    private String productTypeCode; 
    private String nonStockItemFlag;
    private String salableFlag;
    private double weight;
    private double qty;
    private double grossValue;
    private double value;
    private ArrayList<Product> product;


    public Product() {

    }
    public Product(Parcel in) {
         this();
          readFromParcel(in);
    }

       /*
       * Constructor calls read to create object
       */
       private void readFromParcel(Parcel in) {
          in.readTypedList(product, Product.CREATOR); /* NULLPOINTER HERE */
       }

       public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
              dest.writeList(product);
        }

        public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>() { 
            public Product createFromParcel(Parcel in) { 
                Product prod = new Product();
                Bundle b = in.readBundle(Product.class.getClassLoader());        
                prod.product = b.getParcelableArrayList("enteredProduct");

            return prod;
            }

        public Product[] newArray(int size) {
            return new Product[size];
        }
    };

    /**
     * @param product
     */
    public Product(ArrayList<Product> product) {
        this.product = product;
    }

    /**
     * @param availableQuantity
     * @param price
     * @param productCode
     * @param description
     * @param nonStockItemFlag
     * @param kitProductFlag
     * @param qty
     * @param grossValue
     * @param value
     */
    public Product(double availableQuantity, double price, String productCode,
            String description, String nonStockItemFlag, 
            double qty,  double value) {
        super();
        this.availableQuantity = availableQuantity;
        this.price = price;
        this.productCode = productCode;
        this.description = description;
        this.nonStockItemFlag = nonStockItemFlag;

        this.qty = qty;
        this.value = value;
    }

    public ArrayList<Product> getProduct() {
        return product;
    }

    public void setProduct(ArrayList<Product> product) {
        this.product = product;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public double getGrossValue() {
        return grossValue;
    }
    public void setGrossValue(double grossValue) {
        this.grossValue = grossValue;
    }
    public double getQty() {
        return qty;
    }
    public void setQty(double qty) {
        this.qty = qty;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getAvailableQuantity() {
        return availableQuantity;
    }
    public void setAvailableQuantity(double availableQuantity) {
        this.availableQuantity = availableQuantity;
    }
    public String getProductCode() {
        return productCode;
    }
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getDescription2() {
        return description2;
    }
    public void setDescription2(String description2) {
        this.description2 = description2;
    }
    public String getProductGroup() {
        return productGroup;
    }
    public void setProductGroup(String productGroup) {
        this.productGroup = productGroup;
    }
    public String getAlternateSearch() {
        return alternateSearch;
    }
    public void setAlternateSearch(String alternateSearch) {
        this.alternateSearch = alternateSearch;
    }
    public String getProductTypeCode() {
        return productTypeCode;
    }
    public void setProductTypeCode(String productTypeCode) {
        this.productTypeCode = productTypeCode;
    }

    public String getNonStockItemFlag() {
        return nonStockItemFlag;
    }
    public void setNonStockItemFlag(String nonStockItemFlag) {
        this.nonStockItemFlag = nonStockItemFlag;
    }

    public String getSalableFlag() {
        return salableFlag;
    }
    public void setSalableFlag(String salableFlag) {
        this.salableFlag = salableFlag;
    }

    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
  }

And caller Activity :

  if(productMap.size() >0){
         ArrayList<Product> enteredProductList = new ArrayList<Product>(productMap.values());
         Bundle b = new Bundle();
         Product pr =new Product(enteredProductList);
         b.putParcelableArrayList("enteredProduct", enteredProductList);
         Intent showContent = new Intent(getApplicationContext(),RetailerOrderIActivity.class);
         showContent.putExtras(b); //Insert the Bundle object in the Intent' Extras
         startActivity(showContent);
     }else{
         Toast.makeText(RetailerOrderActivity.this," You don't have invoice records" ,Toast.LENGTH_SHORT).show();
     }

and receive part :

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order_main);
    Bundle b = this.getIntent().getExtras();

    ArrayList<Product> p = b.getParcelableArrayList("enteredProduct");
    System.out.println("-- RetailerOrderIActivity --" + p.size());
    for (Product s : p) {
        System.out.println(" --Qty-" + s.getQty());
        System.out.println(" --price -" + s.getPrice());
        System.out.println(" --code -" + s.getProductCode());
    }

This is my error part :

     09-13 16:22:43.236: ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
09-13 16:22:43.236: ERROR/AndroidRuntime(343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xont.controller/com.xont.controller.sales.RetailerOrderIActivity}: java.lang.NullPointerException
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Looper.loop(Looper.java:123)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invoke(Method.java:507)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at dalvik.system.NativeStart.main(Native Method)
09-13 16:22:43.236: ERROR/AndroidRuntime(343): Caused by: java.lang.NullPointerException
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at com.xont.entity.Product$1.createFromParcel(Product.java:94)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at com.xont.entity.Product$1.createFromParcel(Product.java:1)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readParcelable(Parcel.java:1981)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readValue(Parcel.java:1846)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readListInternal(Parcel.java:2092)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readArrayList(Parcel.java:1536)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readValue(Parcel.java:1867)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Parcel.readMapInternal(Parcel.java:2083)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Bundle.unparcel(Bundle.java:208)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at com.xont.controller.sales.RetailerOrderIActivity.onCreate(RetailerOrderIActivity.java:18)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-13 16:22:43.236: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Please help me..its return null ... where is wrong ?please advice me on this?

Thanks in advance

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is how you can pass an ArrayList as I tried it.

MyListClass.java - Custom class

public class MyListClass implements Parcelable{

    private int test;

    public MyListClass()
    {}

    public MyListClass(Parcel read){
        test = read.readInt();
    }

    public int getTest() {
        return test;
    }

    public void setTest(int test) {
        this.test = test;
    }

    public static final Parcelable.Creator<MyListClass> CREATOR = 
        new Parcelable.Creator<MyListClass>() {

            @Override
            public MyListClass createFromParcel(Parcel source) {
                return new MyListClass(source);
            }

            @Override
            public MyListClass[] newArray(int size) {
                return new MyListClass[size];
            }
        };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeInt(test);
    }
}

MyParcelable.java

public class MyParcelable implements Parcelable {

    private List<MyListClass> arrList = new ArrayList<MyListClass>();
    private int myInt = 0;
    private String str = null;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public List<MyListClass> getArrList() {
        return arrList;
    }

    public void setArrList(List<MyListClass> arrList) {
        this.arrList = arrList;
    }

    public int getMyInt() {
        return myInt;
    }

    public void setMyInt(int myInt) {
        this.myInt = myInt;
    }

    MyParcelable() {
        // initialization
        arrList = new ArrayList<MyListClass>();
    }

    public MyParcelable(Parcel in) {
        myInt = in.readInt();
        str = in.readString();
        in.readTypedList(arrList, MyListClass.CREATOR);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel outParcel, int flags) {
        outParcel.writeInt(myInt);
        outParcel.writeString(str);
        outParcel.writeTypedList(arrList);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {

        @Override
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        @Override
        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };
}

MainAcitivty.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        arrList.add(new MyListClass());
        arrList.get(0).setTest(200);

        MyParcelable object = new MyParcelable();
        object.setMyInt(100);
        object.setArrList(arrList);

        Intent intent = new Intent(MainActivity.this,ReceiverParcel.class);
        intent.putExtra("parcel", object);
        startActivity(intent);
    }

ReceiverParcel.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle b = getIntent().getExtras();
        MyParcelable object = b.getParcelable("parcel");

        System.out.println(object.getArrList().get(0).getTest());
        System.out.println(object.getMyInt());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...