如何在parcelable中修复“类型不匹配错误:无法从元素类型转换为浮点数”?

时间:2013-09-24 20:19:21

标签: android parcelable

当用户按下按钮时,我试图将parcelable中的FloatArrayList从主活动传递到“CopyofBarGraph”活动。

以下是主活动中Bundle的代码(当用户按下按钮时发送):

public void barGraphHandler (View view)
{

    Bundle b = new Bundle();
    b.putParcelable("fnumbers", floatarray); 
    Intent barintent = new Intent(this, CopyofBarGraph.class);
    barintent.putExtras(b); 
    startActivity(barintent);

}

Parcelable ArrayList的代码是:

public class FloatArrayList extends ArrayList<fnumber> implements Parcelable{


    private static final long serialVersionUID = 663585476779879096L;
     public FloatArrayList(){
    }

     public FloatArrayList(Parcel in){
            readFromParcel(in);
     }


     @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

            public FloatArrayList createFromParcel(Parcel in) {
                     return new FloatArrayList(in);
             }

            public FloatArrayList[] newArray(int arg0) {
                     return new FloatArrayList[arg0];

            }};

     private void readFromParcel(Parcel in) {
             this.clear();

             int size = in.readInt();


            for (int i = 0; i < size; i++) {

                    fnumber fn = new fnumber();
                     fn.setFnumber(in.readFloat());
                     this.add(fn);
             }
               }


    public int describeContents() {

            return 0;
     }


    public void writeToParcel(Parcel dest, int flags) {
             int size = this.size();
             dest.writeInt(size);


            for (int i = 0; i < size; i++) {

                    fnumber fn = this.get(i);
                     dest.writeFloat(fn.getFnumber());

            }}}  >

对于fnumber,代码是:

public class fnumber {

       private Float fl;
           public fnumber(){
          }

         public fnumber(Float fl){
               this.fl = fl;
             }

           public void setFnumber(Float fl) {
                   this.fl = fl;
           }
                   public Float getFnumber() {
                   return fl;
           }    }    >

直到这里,一切似乎都运转正常。

我的问题是当我尝试在CopyofBarGrpah类中使用FloatArrayList时:

  public class CopyofBarGraph extends Activity{

public FloatArrayList floatarray;

public Intent getintent(Context context) 
{   
    //data

     Bundle b = getIntent().getExtras(); //Get the intent's extras
     floatarray = b.getParcelable("fnumbers"); //get our list

    float[] y = new float[floatarray.size()];
     int index = 0;
     for (float f : floatarray) { // THIS IS THE LINE WHERE I GET THE ERROR: TYPE MISMATCH: CANNOT CONVERT FROM ELEMENT TYPE fnumber TO float.

         y[index++] = f;
     }    

这是我第一次使用Java和Android进行编码,我无法解决问题。 任何帮助或提示都会非常有用。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您似乎拥有FloatArrayList(ArrayList<fnumber>),即fnumbers的集合。但你试着和float一起去。 Java不喜欢这个 请改为:

for (fnumber f : floatarray) { 
//and use f.getFnumber() inside instead
}

答案 1 :(得分:0)

相反:

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

        public FloatArrayList createFromParcel(Parcel in) {
                 return new FloatArrayList(in);
         }

        public FloatArrayList[] newArray(int arg0) {
                 return new FloatArrayList[arg0];

        }};

尝试使用Type写一下:

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

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

        public FloatArrayList[] newArray(int arg0) {
                 return new FloatArrayList[arg0];

        }}; 

之后:

 float[] y = new float[floatarray.size()];
 int index = 0;

 for (fnumber f : floatarray) { 
     y[index++] = fnumber.getFnumber();
 }