尝试在 Cast.writetoParcel 的空对象引用上调用虚拟方法“int.java.lang.Integer.intValue()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45308126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempt to invoke virtual method 'int.java.lang.Integer.intValue()' on a null object reference at Cast.writetoParcel
提问by Lakshya Punhani
Getting Error FATAL EXCEPTION: main Process: com.example.wuntu.tv_bucket, PID: 3895 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.example.wuntu.tv_bucket.Models.Cast.writeToParcel(Cast.java:136) at android.os.Parcel.writeParcelable(Parcel.java:1437) at android.os.Parcel.writeValue(Parcel.java:1343) at android.os.Parcel.writeList(Parcel.java:759) at android.os.Parcel.writeValue(Parcel.java:1365) at android.os.Parcel.writeArrayMapInternal(Parcel.java:686) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at android.os.Bundle.writeToParcel(Bundle.java:1079) at android.os.Parcel.writeBundle(Parcel.java:711) at android.content.Intent.writeToParcel(Intent.java:8790) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3112) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541) at android.app.Activity.startActivityForResult(Activity.java:4284) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) at android.app.Activity.startActivityForResult(Activity.java:4231) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) at android.app.Activity.startActivity(Activity.java:4568) at android.app.Activity.startActivity(Activity.java:4536) at com.example.wuntu.tv_bucket.Adapters.CastDetailAdapter$1.onClick(CastDetailAdapter.java:124) at android.view.View.performClick(View.java:5698) at android.widget.TextView.performClick(TextView.java:10908) at android.view.View$PerformClick.run(View.java:22557) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7231) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
获取错误 FATAL EXCEPTION: main Process: com.example.wuntu.tv_bucket, PID: 3895 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com. example.wuntu.tv_bucket.Models.Cast.writeToParcel(Cast.java:136) at android.os.Parcel.writeParcelable(Parcel.java:1437) at 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) at android.os.Bundle.writeToParcel(Bundle.java:1079) at 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) 在 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)158) 在 android.app.ActivityThread.main(ActivityThread.java:7231) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:第 1230 章158) 在 android.app.ActivityThread.main(ActivityThread.java:7231) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:第 1230 章invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Getting error in sending arraylist
of object from adapter to other activity. I wanna send my arraylist
from onBindViewHolder
method of Adapter to another activity but its is showing null exception error on the Cast Class in writetoParcel
Method. How to send arraylist
properly?
将arraylist
对象从适配器发送到其他活动时出错。我想送我arraylist
从onBindViewHolder
适配器的方法到另一个活动,但它正显示出对在演员类零异常错误writetoParcel
的方法。如何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 Class
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();
}
}
回答by Ben P.
In your writeToParcel()
method, you have
在你的writeToParcel()
方法中,你有
parcel.writeInt(id);
Since id
is Integer
, this is going to auto-unbox id
. If id
is null, this auto-unboxing will throw a NullPointerException
.
由于id
is Integer
,这将自动拆箱id
。如果id
为 null,则此自动拆箱将抛出NullPointerException
.
Since there is no Parcel.writeInteger()
method, you're going to have to record whether or not id
is null in a separate write. Something like:
由于没有Parcel.writeInteger()
方法,您将不得不id
在单独的写入中记录是否为空。就像是:
if (id == null) {
dest.writeInt(0);
}
else {
dest.writeInt(1);
dest.writeInt(id);
}
And to read it back out:
并读出它:
int hasId = in.readInt();
if (hasId == 1) {
id = in.readInt();
}
else {
id = null;
}
回答by Maarten van Tjonger
The order in which you read the values from the parcel has to be the same as the order it was written to it.
您从包裹中读取值的顺序必须与写入包裹的顺序相同。
Try:
尝试:
protected Cast(Parcel in) {
name = in.readString();
profilePath = in.readString();
character = in.readString();
id = in.readInt();
}