2

2 つのアクティビティ (A と B) があります。アクティビティ A は、 を使用してアクティビティ B を起動しstartActivityForResultます。

アクティビティ B から、アクティビティ AI に戻るために次のコードを使用しました。

ArrayList<Articles> selectedArticlesList =null;
...
Intent data = new Intent();
Bundle bundle = new Bundle();  
bundle.putParcelableArrayList("selected-articles", selectedArticlesList);
data.putExtras(bundle);
setResult(RESULT_OK, data);
finish();

クラスがクラスArticlesを実装する場所Parcelable

アクティビティ A はメソッドをオーバーライドしますonActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(data.hasExtra("selected-articles")){
         dettServiziListCorrente = new ArrayList<Articles>();
         dettServiziListCorrente  = data.getExtras().getParcelableArrayList("selected-articles");  
         Articles a = dettServiziListCorrente.get(0);
         Log.i("PRINT DESCRIPTION MEMBER", String.valueOf(a.description));
    }
}

次のエラーが表示されます。

09-11 17:29:24.020: E/AndroidRuntime(21499): FATAL EXCEPTION: main
09-11 17:29:24.020: E/AndroidRuntime(21499): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {host.activity/host.activity.ActivityA}: java.lang.RuntimeException: bad array lengths
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread.access$1100(ActivityThread.java:123)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Looper.loop(Looper.java:137)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at dalvik.system.NativeStart.main(Native Method)
09-11 17:29:24.020: E/AndroidRuntime(21499): Caused by: java.lang.RuntimeException: bad array lengths
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readStringArray(Parcel.java:838)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at host.framework.component.Articles.<init>(Articles.java:30)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at host.framework.component.Articles$1.createFromParcel(Articles.java:56)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at host.framework.component.Articles$1.createFromParcel(Articles.java:1)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readParcelable(Parcel.java:1992)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readValue(Parcel.java:1854)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readListInternal(Parcel.java:2103)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readArrayList(Parcel.java:1544)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readValue(Parcel.java:1875)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Parcel.readMapInternal(Parcel.java:2094)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Bundle.unparcel(Bundle.java:223)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.os.Bundle.containsKey(Bundle.java:264)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.content.Intent.hasExtra(Intent.java:3754)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at host.activity.ACT_CreazioneOrdine.onActivityResult(ACT_CreazioneOrdine.java:192)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
09-11 17:29:24.020: E/AndroidRuntime(21499):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
09-11 17:29:24.020: E/AndroidRuntime(21499):    ... 11 more

Articles クラスの私の実装は次のとおりです。

public class Articles implements Parcelable{

    public int id;
    public String description;
    public String immagine;
    public int quantita;

    public Articles (int id,String descriprion, String immagine){
        this.id = id;
        this.description = descriprion;
        this.immagine = immagine;   
        this.quantita = 0;
    }

    public Articles (int id,String descriprion, String immagine,int quantita){
        this.id = id;
        this.description = descriprion;
        this.immagine = immagine;   
        this.quantita = quantita;
    }

    public Articles (Parcel in){
        String[] data = new String[3];

        in.readStringArray(data);
        this.id = Integer.parseInt(data[0]);
        this.description = data[1];
        this.immagine = data[2];
        this.quantita = Integer.parseInt(data[3]);
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeStringArray(new String[] {
                String.valueOf(this.id),
                this.description,
                this.immagine,
                String.valueOf(this.quantita)
            });
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Articles createFromParcel(Parcel in) {
            return new Articles (in); 
        }

        public Articles [] newArray(int size) {
            return new Articles [size];
        }
    };
     }
4

1 に答える 1

4

問題はこの関数にあります:

public Articles (Parcel in)
{
       String[] data = new String[3];// here you defined array of 3 elements it should be 4
       in.readStringArray(data);
       this.id = Integer.parseInt(data[0]);
       this.description = data[1];
       this.immagine = data[2];
       this.quantita = Integer.parseInt(data[3]);
}

この行では 4 つの要素を渡しているためです。

dest.writeStringArray(new String[]
    { String.valueOf(this.id), this.description, this.immagine, String.valueOf(this.quantita) });
于 2013-09-11T16:54:58.837 に答える