这是我的Adapter类:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.container_accommodation,parent,false);
MyHolder holder = new MyHolder(view);
holder.setIsRecyclable(false);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
MyHolder myHolder= (MyHolder) holder;
Accommodation current = data.get(position);
Picasso.with(context).load("http://10.0.2.2:18180/AAFS/Accommodations/"+current.AccommoID+"/Accomm_Images/"+current.AccommoMainImage.ImageName)
.error(R.drawable.error)
.into(myHolder.imageView);
}
以下是我尝试向上滚动时出现的错误,向下滚动工作正常。
致命的例外:主要 显示java.lang.NullPointerException 在 com.example.phuluso.aafs.AdapterAccommodation.onBindViewHolder(AdapterAccommodation.java:73) 在 android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5471) 在 android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5504) 在 android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4741) 在 android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4617) 在 android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1994) 在 android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) 在 android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) 在 android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1180) 在 android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1031) 在 android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:4061) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 在android.view.Choreographer.doCallbacks(Choreographer.java:555) 在android.view.Choreographer.doFrame(Choreographer.java:524) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4944) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 在dalvik.system.NativeStart.main(本地方法)
任何帮助都会很棒。
感谢。
答案 0 :(得分:0)
public class AdapterAccommodation extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
private Context context;
private LayoutInflater inflater;
List<Accommodation> data = Collections.emptyList();
List<Accommodation> mStringFilterList;
ValueFilter valueFilter;
public AdapterAccommodation(Context context,List<Accommodation>data)
{
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
mStringFilterList = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = inflater.inflate(R.layout.container_accommodation,parent,false);
MyHolder holder = new MyHolder(view);
holder.setIsRecyclable(false);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder= (MyHolder) holder;
Accommodation current = data.get(position);
myHolder.textAccommoName.setText(current.AccommoName);
myHolder.txtAddress.setText("Address: " + current.AccommoAddress.Street + ", " + current.AccommoAddress.Town + ", " + current.AccommoAddress.City);
myHolder.txtGenderAllowed.setText("Gender Accommodated: " + current.AccommoDetails.GenderAllowed );
myHolder.txtDistance.setText("Distance from nearest Campus: " + current.Distance + " km");
myHolder.textCapacity.setText("Capacity: " + current.Capacity);
myHolder.textNearestCampus.setText("Nearest Campus: "+ current.NearestCampus);
Picasso.with(context).load("http://192.168.42.197:18180/AAFS/Accommodations/"+current.AccommoID+"/Accomm_Images/"+current.AccommoMainImage.ImageName)
.error(R.drawable.error)
.into(myHolder.imageView);
myHolder.ratingBar.setRating(current.avgAccommoRating);
myHolder.ratingBar.setIsIndicator(true);
myHolder.btnAdditionalDetails.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,AccommodationAdditionalDetails.class);
intent.putExtra("EXTRA_AccommodationID",data.get(position).AccommoID);
intent.putExtra("EXTRA_AccommodationName",data.get(position).AccommoName);
intent.putExtra("EXTRA_AccommodationCapacity",data.get(position).Capacity);
intent.putExtra("EXTRA_AccommodationNearestCampus",data.get(position).NearestCampus);
intent.putExtra("EXTRA_AccommodationDistance",data.get(position).Distance);
intent.putExtra("EXTRA_AccommodationAccreditedStatus",data.get(position).AccredStatus);
intent.putExtra("EXTRA_AccommodationCity",data.get(position).AccommoAddress.City);
intent.putExtra("EXTRA_AccommodationPostalAddress",data.get(position).AccommoAddress.PostalCode);
intent.putExtra("EXTRA_LATITUDE",data.get(position).AccommoAddress.Lattitude);
intent.putExtra("EXTRA_LONGITUDE",data.get(position).AccommoAddress.Longitude);
intent.putExtra("EXTRA_NUM_SHARING",data.get( position ).AccommoDetails.NumTwoSharingRooms);
intent.putExtra("EXTRA_NUM_SINGLE",data.get(position).AccommoDetails.NumSingleRooms);
intent.putExtra("EXTRA_RENT_SINGLE",data.get(position).AccommoDetails.RentSingle);
intent.putExtra("EXTRA_RENT_SHARING",data.get(position).AccommoDetails.RentSharing);
intent.putExtra("EXTRA_WIFI",data.get(position).AccommoDetails.WiFi);
intent.putExtra("EXTRA_LIBRARY",data.get(position).AccommoDetails.DedicatedStudyArea);
intent.putExtra("EXTRA_GYM",data.get(position).AccommoDetails.Gym);
intent.putExtra("EXTRA_SECURITY_LEVEL",data.get(position).AccommoDetails.SecurityLevel);
intent.putExtra("EXTRA_PARKING_LOT",data.get(position).AccommoDetails.ParkingPort);
intent.putExtra("EXTRA_GENDER",data.get(position).AccommoDetails.GenderAllowed);
intent.putExtra("EXTRA_TRANSPORT",data.get(position).AccommoDetails.TransportShuttle);
intent.putExtra("EXTRA_WASHING_MACHINE",data.get(position).AccommoDetails.WashingMachine);
intent.putExtra("EXTRA_NUM_BATHROOM",data.get( position ).AccommoDetails.NumBathrooms);
intent.putExtra("EXTRA_CLEANING_SERVICE",data.get( position ).AccommoDetails.CleaningService);
context.startActivity(intent);
SharedPreferences settings = enter code herePreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ACCOMMO_LATITUDE",data.get(position).AccommoAddress.Lattitude );
editor.commit();
SharedPreferences setting1 = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor1 = settings.edit();
editor.putString("ACCOMMO_LONGITUDE",data.get(position).AccommoAddress.Longitude );
editor.commit();
}
} );
}
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView textAccommoName;
// ImageView ivAccommodation;
TextView textCapacity;
TextView textNearestCampus;
TextView txtAddress;
TextView txtGenderAllowed;
TextView txtDistance;
Button btnAdditionalDetails;
RatingBar ratingBar;
ImageView imageView ;
public MyHolder(View itemView) {
super(itemView);
textAccommoName = (TextView)itemView.findViewById(R.id.textAccommodationName);
txtAddress = (TextView)itemView.findViewById(R.id.txtAddress );
txtGenderAllowed = (TextView)itemView.findViewById( R.id.txtGenderAllowed );
txtDistance = (TextView)itemView.findViewById( R.id.textDistanceFromNearestCampus );
imageView = (ImageView)itemView. findViewById(R.id.imageview);
// ivAccommodation = (ImageView)itemView.findViewById(R.id.img);
textCapacity = (TextView)itemView.findViewById(R.id.textCapacity);
textNearestCampus = (TextView)itemView.findViewById(R.id.textNearestCampus);
btnAdditionalDetails = (Button)itemView.findViewById(R.id.btnAdditionalDetails);
ratingBar = (RatingBar)itemView.findViewById(R.id.ratingBar);
}
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Accommodation> filterList = new ArrayList<Accommodation>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).AccommoName.toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
Accommodation accommodation = new Accommodation();
accommodation.AccommoName = mStringFilterList.get(i).AccommoName;
accommodation.AccommoDetails.GenderAllowed = mStringFilterList.get(i).AccommoDetails.GenderAllowed;
accommodation.AccommoAddress.Town = mStringFilterList.get(i).AccommoAddress.Town;
accommodation.AccommoAddress.City = mStringFilterList.get(i).AccommoAddress.City;
accommodation.Capacity = mStringFilterList.get(i).Capacity;
accommodation.Distance = mStringFilterList.get(i).Distance;
accommodation.AccommoAddress.Street = mStringFilterList.get(i).AccommoAddress.Street;
accommodation.NearestCampus = mStringFilterList.get(i).NearestCampus;
accommodation.avgAccommoRating = mStringFilterList.get(i).avgAccommoRating;
filterList.add(accommodation);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
data = (ArrayList<Accommodation>) results.values;
notifyDataSetChanged();
}
}
}
答案 1 :(得分:0)
您是否覆盖了适配器中的getItemCount方法?
@Override
public int getItemCount(){
return data.size();
}
似乎onBindViewHolder(VH holder, int position)
方法提供了无效的位置参数。因此,当您从数据集合中获取Accommodation时,它将返回null。您使用什么类型的收藏品来容纳住宿?
答案 2 :(得分:0)
好的,现在我看到你的适配器代码我认为问题很可能在于你的过滤器实现。尝试将publishResults
方法更改为:
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if(results.count > 0){
data.clear();
data.addAll(results.values);
notifyDataSetChanged();
}else{
notifyDataSetInvalidated();
}
}
已编辑 - 尝试访问其中一个Accommodation对象上的AccommoMainImage属性时发生NPE。
添加了空检查以解决问题:
if(current.AccommoMainImage != null){
Picasso.with(context).load("http://10.0.2.2:18180/AAFS/Accommodations/"+current.AccommoID+"/Accomm_Images/"+current.AccommoMainImage.ImageName) .error(R.drawable.error) .into(myHolder.imageView);
}