4

パーセル可能なオブジェクトでインテントを送信しているときに、奇妙な問題が発生しました。このオブジェクトには1つの文字列配列があります。この回答の使用インテントを使用して、あるAndroidアクティビティから別のAndroidアクティビティにオブジェクトを送信するにはどうすればよいですか?最初にオブジェクトをパーセル可能として作成してから送信しました。これは私のコードの断片です:

    public class MyObject implements Parcelable{

    private String _title = null;
    private String[] _genre;
    private int _time = 0;
    private String _disc = null;

    private static final String TAG = "MyObject";

    public MyObject (String title, String[] genre, int time, String disc) {
        _title = title;
        _genre = genre;
        _time = time;
        _disc = disc;
    }



    public MyObject(Parcel source) {
            /*
             * Reconstruct from the Parcel
             */
            Log.v(TAG, "ParcelData(Parcel source): time to put back parcel data");
            _title = source.readString();
            try{
                source.readStringArray(_genre);
            }catch(Exception e){
                Log.e(TAG,e.toString());
            }
        _time = source.readInt();
        _disc = source.readString();
    }

    /*... Getters and setters and other stuff...*/

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Log.v(TAG, "writeToParcel..."+ flags);
        dest.writeString(_title);
        dest.writeStringArray(_genre);
        dest.writeInt(_time);
        dest.writeString(_disc);
        Log.i(TAG,_genre[0]);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in);
        }

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

送信は問題ないようです。

Intent displayInfo = new Intent(getApplicationContext(), MovieInfo.class);
                    displayInfo.putExtra(OBJECT, mySQLiteAdapter.getEntry(idSet));
                    startActivity(displayInfo);

しかし、私がこのデータを取得しようとしているとき

Intent intent = getIntent();
        myParcel = (MyObject) intent.getParcelableExtra(FBM2Activity.OBJECT);

readStringArrayからNullPointerExceptionを取得しています。どこで間違いを犯したのか誰か知っていますか?

4

1 に答える 1

8

を使用し_genre = source.createStringArray()ます。

この背後にある理由は、正しい数の配列要素ですでに作成されていることをreadStringArray(String[] val)期待valしているということです。String[]あなたがそれをしている方法_genrenull、あなたが電話するときですsource.readStringArray(_genre)

于 2012-07-15T15:31:07.407 に答える