尝试调用虚方法' int.java.lang.Integer.intValue()'在Cast.writetoParcel

时间:2017-07-25 15:50:25

标签: java android arraylist android-recyclerview

Error Image

  

获得错误   致命异议:主要                                                                              处理:com.example.wuntu.tv_bucket,PID:3895                                                                              java.lang.NullPointerException:尝试调用虚方法' int java.lang.Integer.intValue()'在null对象引用上                                                                                  在com.example.wuntu.tv_bucket.Models.Cast.writeToParcel(Cast.java:136)                                                                                  在android.os.Parcel.writeParcelable(Parcel.java:1437)                                                                                  在android.os.Parcel.writeValue(Parcel.java:1343)                                                                                  在android.os.Parcel.writeList(Parcel.java:759)                                                                                  在android.os.Parcel.writeValue(Parcel.java:1365)                                                                                  在android.os.Parcel.writeArrayMapInternal(Parcel.java:686)                                                                                  在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)                                                                                  在android.os.Bundle.writeToParcel(Bundle.java:1079)                                                                                  在android.os.Parcel.writeBundle(Parcel.java:711)                                                                                  在android.content.Intent.writeToParcel(Intent.java:8790)                                                                                  在android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3112)                                                                                  在android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)                                                                                  在android.app.Activity.startActivityForResult(Activity.java:4284)                                                                                  在android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)                                                                                  在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)                                                                                  在android.app.Activity.startActivityForResult(Activity.java:4231)                                                                                  在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)                                                                                  在android.app.Activity.startActivity(Activity.java:4568)                                                                                  在android.app.Activity.startActivity(Activity.java:4536)                                                                                  在com.example.wuntu.tv_bucket.Adapters.CastDetailAdapter $ 1.onClick(CastDetailAdapter.java:124)                                                                                  在android.view.View.performClick(View.java:5698)                                                                                  在android.widget.TextView.performClick(TextView.java:10908)                                                                                  在android.view.View $ PerformClick.run(View.java:22557)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:158)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:7231)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

从适配器向其他活动发送arraylist对象时出错。我想将arraylist从适配器的onBindViewHolder方法发送到另一个活动,但它在writetoParcel方法中的Cast类上显示空异常错误。如何正确发送arraylist

Cast Class

public class Cast implements Parcelable {

    @SerializedName("cast_id")
    @Expose
    private Integer castId;
    @SerializedName("character")
    @Expose
    private String character;
    @SerializedName("credit_id")
    @Expose
    private String creditId;
    @SerializedName("gender")
    @Expose
    private Integer gender;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("order")
    @Expose
    private Integer order;
    @SerializedName("profile_path")
    @Expose
    private String profilePath;

    public Cast(){

    }

    protected Cast(Parcel in) {
        character = in.readString();
        id = in.readInt();
        name = in.readString();
        profilePath = in.readString();
    }

    public static final Creator<Cast> CREATOR = new Creator<Cast>() {
        @Override
        public Cast createFromParcel(Parcel in) {
            return new Cast(in);
        }

        @Override
        public Cast[] newArray(int size) {
            return new Cast[size];
        }
    };

    public Integer getCastId() {
        return castId;
    }

    public void setCastId(Integer castId) {
        this.castId = castId;
    }

    public String getCharacter() {
        return character;
    }

    public void setCharacter(String character) {
        this.character = character;
    }

    public String getCreditId() {
        return creditId;
    }

    public void setCreditId(String creditId) {
        this.creditId = creditId;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public String getProfilePath() {
        return profilePath;
    }

    public void setProfilePath(String profilePath) {
        this.profilePath = profilePath;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i)
    {
        parcel.writeString(name);
        parcel.writeString(profilePath);
        parcel.writeString(character);
        parcel.writeInt(id);
    }
}

CastDetailAdapter类

public class CastDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Cast> detailArrayList = new ArrayList<>() ;
    private UrlConstants urlConstants = UrlConstants.getSingletonRef();
    private Cast cast;
    private final int VIEW_ITEM = 0;
    private final int VIEW_PROG = 1;
    private Context context;
    MovieView a;
    ArrayList<Cast> FullArrayList = new ArrayList<>();

    public CastDetailAdapter(MovieView movieView, ArrayList<Cast> detailArrayList,ArrayList<Cast> subCastArrayList)
    {
        a = movieView;
        this.detailArrayList = subCastArrayList;
        this.FullArrayList = detailArrayList;
    }


    public class MyViewHolder1 extends RecyclerView.ViewHolder
    {
        ImageView cast_profile_picture;
        TextView cast_name,cast_character_name;

        public MyViewHolder1(View view)
        {
            super(view);
            cast_profile_picture = (ImageView) view.findViewById(R.id.thumbnail);
            cast_name = (TextView) view.findViewById(R.id.title);
            cast_character_name = (TextView) view.findViewById(R.id.count);
        }
    }

    public class FooterViewHolder1 extends RecyclerView.ViewHolder
    {
        TextView view_more;

        public FooterViewHolder1(View itemView) {
            super(itemView);
            view_more = (TextView) itemView.findViewById(R.id.view_more);


        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionItem(position))
            return VIEW_ITEM;
        return VIEW_PROG;
    }

    private boolean isPositionItem(int position) {
        return position != getItemCount() -1;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        context = parent.getContext();

        if (viewType == VIEW_ITEM)
        {
            View v =  LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.cast_details, parent, false);
            return new MyViewHolder1(v);

        } else if (viewType == VIEW_PROG){
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.footer_layout_movie_details, parent, false);
            return new FooterViewHolder1(v);
        }
        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if(holder instanceof MyViewHolder1)
        {
            cast = detailArrayList.get(position);
            ((MyViewHolder1)holder).cast_character_name.setText(cast.getCharacter());
            ((MyViewHolder1)holder).cast_name.setText(cast.getName());
            String url3 = urlConstants.URL_Image + cast.getProfilePath();
            Picasso.with(context)
                    .load(url3)
                    .into(((MyViewHolder1)holder).cast_profile_picture);
        }
        else if (holder instanceof FooterViewHolder1)
        {
            ((FooterViewHolder1)holder).view_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    Intent intent = new Intent(context,CastViewActivity.class);
                    intent.putParcelableArrayListExtra("LIST",FullArrayList);
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return this.detailArrayList.size();
    }
}

2 个答案:

答案 0 :(得分:2)

writeToParcel()方法中,您有

parcel.writeInt(id);

由于idInteger,因此会自动取消装箱id。如果id为空,则此自动取消装箱将抛出NullPointerException

由于没有Parcel.writeInteger()方法,因此您必须在单独的写入中记录id是否为空。类似的东西:

if (id == null) {
    dest.writeInt(0);
}
else {
    dest.writeInt(1);
    dest.writeInt(id);
}

然后再读出来:

int hasId = in.readInt();

if (hasId == 1) {
    id = in.readInt();
}
else {
    id = null;
}

答案 1 :(得分:1)

您从包裹中读取值的顺序必须与写入其中的顺序相同。

尝试:

protected Cast(Parcel in) {
    name = in.readString();
    profilePath = in.readString();
    character = in.readString();
    id = in.readInt();
}