2013-10-14 23 views
0

我想通過ArrayList<Polygon>使用intent.putExtra()ArrayList可串行化Android

我得到IOException寫入可序列化的對象(名稱= Polygon.polygon)。

多邊形是我製作的類,並且已經在這個類上實現了Serializable。

Intent intent = new Intent(this, Main.class); 
intent.putExtra("polygons", (ArrayList<Polygon>)this.polygons); 

startActivity(intent); 

Polygon類有一個ArrayList<Point>持有點的名單有幾個getter和setter多邊形,僅此而已。我已經在Polygon類上實現了Serializable,但是我從上面的代碼中得到了這個異常。

public class Polygon implements Serializable{ 

    ArrayList<Point> polygon; 

    public Polygon(){ 
     polygon = new ArrayList(); 
    } 

    public int getSize(){ 
     return polygon.size(); 
    } 

    public ArrayList<Point> getPointArray(){ 
     return this.polygon; 
    } 

    public void addPoint(int x, int y, int mapWidth, int mapHeight, int canvasWidth, int canvasHeight){ 
     //going to scale up here 
     float scaleX = mapWidth/canvasWidth; 
     float scaleY = mapHeight/canvasHeight; 

     polygon.add(new Point((int)(x*scaleX),(int)(y*scaleY))); 
    } 

    public void removePoint(int index){ 
     polygon.remove(index); 
    } 
} 

任何想法?

+0

沒有發佈實施(代碼),你可能不會得到很多有益的幫助。 – Geobits

+0

HI我已經添加了一些代碼。 – jonny

+0

那麼,當我說的時候,我的意思是來自處理序列化的'Polygon'類的代碼。 **但是,**,因爲你發佈了什麼'this.polygons',你爲什麼需要將它轉換爲'ArrayList '?我認爲它已經是那種類型。 – Geobits

回答

0

如果它是您在Polygon類中使用的android.graphics.Point,那麼您應該使用Parcelable,因爲Point實現了Parcelable而不是Serializable。

如果使用需要下列權限添加到您的清單系列化:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>