我想在用戶按下一個按鈕時從主活動中將Parcelable中的FloatArrayList傳遞給「CopyofBarGraph」活動。如何解決「類型不匹配錯誤:無法從元素類型轉換爲浮動」的問題?
下面是在主要活動對束中的代碼(發送當用戶按下按鈕):
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;
} } >
直到這裏,一切似乎工作正常。
我的問題是,當我嘗試使用FloatArrayList在CopyofBarGrpah類:
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的編碼,我只是不能修復該問題。 任何幫助或提示將非常有幫助。
在此先感謝。
它工作:)非常感謝 – DanielaTahnee