0

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

这是 Main Activity 中 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 进行编码,但我无法解决这个问题。任何帮助或提示都会非常有帮助。

提前致谢。

4

2 回答 2

1

似乎您有 FloatArrayList (即ArrayList<fnumber>),即fnumbers. 但你试着去float。而 Java 不喜欢这个
Try 相反:

for (fnumber f : floatarray) { 
//and use f.getFnumber() inside instead
}
于 2013-09-24T20:28:43.567 回答
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();
 }    
于 2013-09-24T20:29:48.990 回答