获取getparcelablearraylistextra在nullpointerexception中返回?

x0fgdtte  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(417)

包裹类别为:

package mobile.bh.classes;

import java.util.List;

import mobile.bh.activities.MethodStep;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

//simple class that just has one member property as an example
public class Recipe implements Parcelable {
    public int id;
    public String name;
    public List<Ingredient> ingredients;
    public List<MethodStep> method;
    public String comment;
    public String image;
    public Bitmap image2;
    Context mContext;

    public Recipe(Context c){
        mContext = c;
    }
    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeString(name);
        out.writeTypedList(ingredients);
        out.writeTypedList(method);
        out.writeString(comment);
        out.writeString(image);
    }

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

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

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private Recipe(Parcel in) {
        id = in.readInt();
        name =in.readString();
        in.readTypedList(ingredients, Ingredient.CREATOR);
        in.readTypedList(method, MethodStep.CREATOR);
        comment = in.readString();
        image = in.readString();
    }

}

发送数据的目的如下:

Intent i = new Intent(context,RecipeInfoActivity.class);
        ArrayList<Recipe> dataList = new ArrayList<Recipe>();
        dataList.add(recipe);
        i.putParcelableArrayListExtra("recipeObj", dataList);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

并在另一端接收数据,这使得nullpointerexceotion

ArrayList<Recipe> p =  getIntent().getParcelableArrayListExtra("recipeObj");

logcat输出:

08-24 16:37:51.172: E/AndroidRuntime(976): FATAL EXCEPTION: main
08-24 16:37:51.172: E/AndroidRuntime(976): java.lang.RuntimeException: Unable to start activity ComponentInfo{mobile.bh/mobile.bh.activities.RecipeInfoActivity}: java.lang.NullPointerException
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Looper.loop(Looper.java:123)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-24 16:37:51.172: E/AndroidRuntime(976):  at java.lang.reflect.Method.invokeNative(Native Method)
08-24 16:37:51.172: E/AndroidRuntime(976):  at java.lang.reflect.Method.invoke(Method.java:521)
08-24 16:37:51.172: E/AndroidRuntime(976):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-24 16:37:51.172: E/AndroidRuntime(976):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-24 16:37:51.172: E/AndroidRuntime(976):  at dalvik.system.NativeStart.main(Native Method)
08-24 16:37:51.172: E/AndroidRuntime(976): Caused by: java.lang.NullPointerException
08-24 16:37:51.172: E/AndroidRuntime(976):  at mobile.bh.classes.Recipe.<init>(Recipe.java:59)
08-24 16:37:51.172: E/AndroidRuntime(976):  at mobile.bh.classes.Recipe.<init>(Recipe.java:56)
08-24 16:37:51.172: E/AndroidRuntime(976):  at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:47)
08-24 16:37:51.172: E/AndroidRuntime(976):  at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:1)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readParcelable(Parcel.java:1906)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readValue(Parcel.java:1771)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readListInternal(Parcel.java:2017)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readArrayList(Parcel.java:1461)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readValue(Parcel.java:1792)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Parcel.readMapInternal(Parcel.java:2008)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Bundle.unparcel(Bundle.java:208)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.content.Intent.getParcelableArrayListExtra(Intent.java:3424)
08-24 16:37:51.172: E/AndroidRuntime(976):  at mobile.bh.activities.RecipeInfoActivity.onCreate(RecipeInfoActivity.java:56)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 16:37:51.172: E/AndroidRuntime(976):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-24 16:37:51.172: E/AndroidRuntime(976):  ... 11 more
aiazj4mn

aiazj4mn1#

我就是这样实现的 Parcelable 用我的 Lists :

public String tag;
public int success;
public int error;
public User user;
public Event event;
public String error_message;    
public List<Event> finishedevents;
public List<Leadevent> leaderboard; 
public List<User> user_search;

public void readFromParcel(Parcel in) {
    tag = in.readString();
    success = in.readInt();
    error = in.readInt();
    user = (User) in.readValue(User.class.getClassLoader());
    event = (Event) in.readValue(Event.class.getClassLoader());
    finishedevents = (ArrayList<Event>) in.readSerializable();
    leaderboard = (ArrayList<Leadevent>) in.readSerializable();
    user_search = (ArrayList<User>) in.readSerializable();
    error_message = in.readString();
}

public void writeToParcel(Parcel out, int content) {
    // TODO Auto-generated method stub
    out.writeString(tag);
    out.writeInt(success);
    out.writeInt(error);
    out.writeValue(user);
    out.writeValue(event);
    out.writeList(finishedevents);
    out.writeList(leaderboard);
    out.writeList(user_search);
    out.writeString(error_message);
}

所以你应该试试 readSerializable() 为了 Lists .

相关问题