2013-03-19 2 views
2

내가 다음 클래스가 개체 :역 직렬화 이미지의 배열

:

public class DetailedProduct implements Serializable { 
    //attributes + get and set 
    private Colour colour; 
    //get+set 

    public class Colour implements Serializable{ 
    private ArrayList<Image> images; 
    //get+set 

    public Image[] getImages() { 
     return images.toArray(new Image[images.size()]); 
    } 
    } 

    public class Image implements Serializable{ 

    private static final long serialVersionUID = 3460333138445770749L; 
    private String image1; 
    private String image2; 
    private String image3; 

     //get/set methods 
    } 
} 

내가 ImageGallery 클래스에서

DetailedProduct.Colour mCurrentColour; 
    Intent myIntent = new Intent(DetailsActivity.this, ImageGallery.class); 
    myIntent.putExtra("Images", mCurrentColour.getImages()); 
    startActivity(myIntent); 

을 다음과 같은 후 인 텐트를 만들어 지금 나는 다음과 같은 코드를 시도

Serializable extras = getIntent().getSerializableExtra("Images"); 
    if (extras != null) { 
     images = (Image[]) extras; 
    } 

하지만 다음 예외가 있습니다 : java.lang.RuntimeException : U java.lang.ClassCastException가 : java.lang.Object 상위 [] I 캐스팅 수있는 방법 com.productdetails.DetailedProduct $ 이미지 []

캐스트 할 수없는 네이블은 ComponentInfo {com./com.productdetails.ProductImageGallery} 활동을 시작합니다 정확하게 배열의 배열에 직렬화 가능

+0

getImages() 코드를 추가하고 최소 액티비티를 연결하십시오. – Raffaele

+0

@Raffaele 업데이트를 참조하십시오 –

+0

정말 내부 클래스가 필요합니까 (예 : 비 정적)? – Raffaele

답변

0

간단히 생각하면, 이미지를 bytearray []로 간단하게 만들 수 있고 바이트 배열은 직렬화 가능한 기본 유형이므로.

0

당신 캐스트

java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.productdetails.DetailedProduct$Image[] 

당신은 의도에 ListArray를 제공하고

로 교체 배열

이 라인을 캐스팅해야 할

if (extras != null) { 
     images = (Image[]) extras; 
    } 

정확하지 올바르지 않습니다

if (extras != null) { 
     images = (ListArray<Image>) extras; 
    } 
+1

'Object []'는'Image []'로 형 변환 될 수 없습니다. 그것은 객체리스트이며,리스트가 아니라는 것을 의미합니다. – Raffaele

0

이미지를 arrayList로 전달했습니다. 그러나 다음과 같은 코드를 안드로이드 런타임 배열을 직렬화하는 데 실패

Serializable extras = getIntent().getSerializableExtra("Images"); 
    if (extras != null) { 
     images = (ArrayList<Image>) extras; 
    } 
+0

그는'Colour.getImages()'의 구현을 보여주지 않습니다. 리스트 멤버뿐만 아니라 배열을 반환 할 수 있습니다. – Raffaele

0

에게 .. 배열로 사용을하고 있습니다. 배열 유형은 여전히 ​​Serializable이지만, Intent에서 사용하는 번들에서 돌아 왔을 때 유형은 항상 Ljava.lang.Object입니다. 어떤 답변은 인터넷에서 Parcelable을 사용하는 것이 좋습니다. 그러나 시도해 보았습니다. 성공하지 못했습니다.

Parcelable 개체 컬렉션을 전송하지만 ArrayList 형식으로 전송할 수 있습니다. 이는 원본 샘플에서 사용하는 데이터 구조이기도합니다. 호출 코드는 다음과 같습니다.

Intent intent = new Intent(this, Target.class); 
ArrayList<Foo> data = new ArrayList<Foo>(); 
data.add(new Foo()); 

intent.putParcelableArrayListExtra("data", data); 
intent.putExtra("serializable", new Foo[] {new Foo()}); 
startActivity(intent); 

보통의 더미 클래스 인 Foo,하지만 Parcelable을 구현하는 것은 매우 지루합니다. 또한 클래스에 Parcelable.Creator<T> 유형의 CREATOR이라는 static 필드가 있어야하며 내부 유형의 경우 Java 언어에서는 전혀 사용할 수 없습니다. 그렇기 때문에 중첩 된 (정적) 클래스로 전환해야하며, 결과적으로 현재 외부 인스턴스를 생성자에 전달하고 그에 따라 필드를 가시 거리로 변경합니다 (private에서 public 또는 package-private). 그건 그렇고 커플 링을 줄이기 때문에 코드베이스의 테스트 가능성도 향상됩니다.내 말하는 대신 uncompilable 자료이며,이 같은

public class Product implements Parcelable { 
    public class Color implements Parcelable { 
    public static final Parcelable.Creator<Color> CREATOR; // Illegal!!! 
    } 
} 

뭔가

public class Product implements Parcelable{ 
    public static class Color implements Parcelable { 
    public Color(Product exOuter) { 
    } 
    } 
} 

하지만이 방법 당신은 포함하는 클래스의 비 정적 멤버에 액세스 할 수 없다는 점에 유의, 따라서 을 Product 밖으로 이동하는 것이 더 좋습니다.