我很难在其他对象中获取OBJECT实例的值。我想要的只是在评论中获得" Email"(凭据)的价值。它在我的CustomListViewAdapter中给了我NullPointerException。
这是我的代码: 评论对象:
public class Comments implements Parcelable {
public int _id;
public String currentTimeStamp;
public String commentContent;
public LocationsData locationsData;
public Credentials credentials;
public Comments() {
super();
}
public Comments( String currentTimeStamp, String commentContent, LocationsData locationsData, Credentials credentials) {
this.currentTimeStamp = currentTimeStamp;
this.commentContent = commentContent;
this.locationsData = locationsData;
this.credentials = credentials;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getCurrentTimeStamp() {
return currentTimeStamp;
}
public void setCurrentTimeStamp(String currentTimeStamp) {
this.currentTimeStamp = currentTimeStamp;
}
public String getCommentContent() {
return commentContent;
}
public void setCommentContent(String commentContent) {
this.commentContent = commentContent;
}
public LocationsData getLocationsData() {
return locationsData;
}
public void setLocationsData(LocationsData locationsData) {
this.locationsData = locationsData;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
public static Creator<Comments> getCREATOR() {
return CREATOR;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this._id);
dest.writeString(this.currentTimeStamp);
dest.writeString(this.commentContent);
dest.writeParcelable(this.locationsData, flags);
dest.writeParcelable(this.credentials, flags);
}
protected Comments(Parcel in) {
this._id = in.readInt();
this.currentTimeStamp = in.readString();
this.commentContent = in.readString();
this.locationsData = in.readParcelable(LocationsData.class.getClassLoader());
this.credentials = in.readParcelable(Credentials.class.getClassLoader());
}
public static final Parcelable.Creator<Comments> CREATOR = new Parcelable.Creator<Comments>() {
@Override
public Comments createFromParcel(Parcel source) {
return new Comments(source);
}
@Override
public Comments[] newArray(int size) {
return new Comments[size];
}
};
}
这是我的凭证对象类
public class Credentials implements Parcelable {
public long _id;
public String password;
public String email;
public Credentials() {
}
public Credentials(String email,String password) {
this.password=password;
this.email= email;
}
public Credentials(long id,String email,String password) {
this._id=id;
this.email=email;
this.password=password;
}
public long get_id() {
return _id;
}
public void set_id(long _id) {
this._id = _id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this._id);
dest.writeString(this.password);
dest.writeString(this.email);
}
protected Credentials(Parcel in) {
this._id = in.readInt();
this.password = in.readString();
this.email = in.readString();
}
public static final Parcelable.Creator<Credentials> CREATOR = new Parcelable.Creator<Credentials>() {
@Override
public Credentials createFromParcel(Parcel source) {
return new Credentials(source);
}
@Override
public Credentials[] newArray(int size) {
return new Credentials[size];
}
};
}
我的CustomListViewAdapter类:
public class CommentsAdapter extends ArrayAdapter<Comments>{
ArrayList<Comments> commentses;
Context ctx;
Credentials credentials= new Credentials();
public CommentsAdapter(Context context, int resource) {
super(context, resource);
this.ctx=context;
}
public CommentsAdapter(Context context, int resource, ArrayList<Comments> commentsArrayList) {
super(context, resource,commentsArrayList);
this.commentses=commentsArrayList;
this.ctx=context;
this.credentials=credentials;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Comments comments= getItem(position);
LayoutInflater inflater= LayoutInflater.from(ctx);
CommentsViewholder holder;
if (convertView == null) {
holder= new CommentsViewholder();
convertView= inflater.inflate(R.layout.customcomment_layout,parent,false);
holder.email= (TextView)convertView.findViewById(R.id.customlistview_username);
holder.commentContent= (TextView)convertView.findViewById(R.id.customlistview_commentcontent);
holder.timeStamp= (TextView)convertView.findViewById(R.id.customlistview_timestamp);
}else {
holder=(CommentsViewholder)convertView.getTag();
}
credentials.getEmail();
comments.setCredentials(credentials);
holder.email.setText(comments.getCredentials().getEmail()); //-----NullPointerException
holder.commentContent.setText(comments.getCommentContent());
holder.timeStamp.setText(comments.getCurrentTimeStamp());
return convertView;
}
private static class CommentsViewholder{
TextView email;
TextView commentContent;
TextView timeStamp;
}
}
这是NPE遇到CustomAdapter中的哪些点
holder.email.setText(comments.getCredentials()getEmail()); // -----的NullPointerException
FATAL EXCEPTION: main
Process: onetown.otop.onetownoneproduct, PID: 29669
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView onetown.otop.onetownoneproduct.Classes.CommentsAdapter$CommentsViewholder.email' on a null object reference
at onetown.otop.onetownoneproduct.Classes.CommentsAdapter.getView(CommentsAdapter.java:61)
at android.widget.AbsListView.obtainView(AbsListView.java:2467)
at android.widget.ListView.onMeasure(ListView.java:1166)
at android.view.View.measure(View.java:17915)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.widget.ScrollView.onMeasure(ScrollView.java:342)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1244)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:631)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2731)
at android.view.View.measure(View.java:17915)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2413)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2105)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1293)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6599)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
答案 0 :(得分:0)
您的崩溃不是由comments.getCredentials().getEmail()
引起的。
问题是您没有在convertedView
上设置标记。
if (convertView == null) {
holder= new CommentsViewholder();
convertView= inflater.inflate(R.layout.customcomment_layout,parent,false);
holder.email= (TextView)convertView.findViewById(R.id.customlistview_username);
holder.commentContent= (TextView)convertView.findViewById(R.id.customlistview_commentcontent);
holder.timeStamp= (TextView)convertView.findViewById(R.id.customlistview_timestamp);
//here you have to set the holder on the convertView.
convertView.setTag(holder);
} else {
holder=(CommentsViewholder)convertView.getTag();
}
在您的实现中,行convertView.getTag()
将返回null,然后当您调用holder.email时,您将获得NPE。