当前位置: 首页 > 知识库问答 >
问题:

再次向上滚动后,带有滑动导致空指针的RecyclerView

彭博厚
2023-03-14

这是我的适配器类:

@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);

    }

这是我尝试再次向上滚动时遇到的错误,向下滚动工作正常。

致命的例外:android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder中的主java.lang.NullPointerException位于com.example.phuluso.aafs.AdapterAccommodation.OnBindBViewHolder(AdapterAcommodation.java:73)位于android.Supports.v7.widget.Widgets.RecyclerViewHolder$Adapter.onBindViewHolder(RecyclerView.java:5471)位于android.support.v7.Wicket.WidgetRecyclerviewView.RecycerView$Adapper(RecyclerView.java:4741)在android.support.v7.widget.RecyclerViewer$Recycler.getViewForPosition(RecyclerView.java:4617)在android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayotManager.java:1994)在android.support.v7.Wicket.LinearLayoutManager.layoutChunk(LinearlayutManager.java:1390)在andro.support.v7.wicket.Linear LayoutManagerandroid.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayotManager.java:1180)位于android.Supports.v7.widget.LinearLayutManager.scrollBy(LinearLayoutManager.java:1031)位于android.support.v7.Wicket.RecyclerView$ViewFlinger.run(RecyclerViewView.java:4061)位于android.;view.;Choreographer$CallbackRecord.run($Choreogropher.java:725)位于android在android.view.Choreographer$FrameDisplayEventReceiver.run(Choreogropher.java:711)在android.os.Handler.handleCallback(Handler.java:615)在android.os.Handler.dispatchMessage(Handlder.java:92)在andro.os.Looper.loop(Looper.java:137)在and卓.app.ActivityThread.main(ActivityThead.java:4944)在java.lang.reflect.Method。java.lang.reflect.Method.invoke(Method.java:511)处的Invokenactive(本机方法)位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)处,com.andro.internal.os.ZygoteInit.main(ZygoteInit.java:805)位于dalvik.system.NativeStart处。主(本机方法

任何帮助都会很好。

谢谢

共有3个答案

岳亮
2023-03-14

好了,现在我看到了您的适配器代码,我认为问题很可能出在您的过滤器实现上。尝试将您的< code>publishResults方法更改为:

@Override
protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
    if(results.count > 0){
        data.clear();
        data.addAll(results.values);
        notifyDataSetChanged();
    }else{
        notifyDataSetInvalidated();
    }
}

已编辑 - 在尝试访问其中一个住宿对象上的 AccommoMainImage 属性时发生 NPE。

添加了空检查来解决问题:

if(current.AccommoMainImage != null){
    Picasso.with(context).load("http://10.0.2.2:18180/AAFS/Accom‌​modations/"+current.‌​AccommoID+"/Accomm_I‌​mages/"+current.Acco‌​mmoMainImage.ImageNa‌​me) .error(R.drawable.error) .into(myHolder.imageView);
}
微生弘
2023-03-14
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();
        }
}


}
别兴国
2023-03-14

是否重写了适配器中的 getItemCount 方法?

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

似乎onBindViewHolder(VH holder,int-position)方法提供了一个无效的位置参数。因此,当您从数据收集中获得调节时,它将返回null。你用什么样的收藏品来存放住宿?

 类似资料:
  • 问题内容: 触摸区域http://imageshack.us/a/img836/2909/swipe1.png 用户可以在绿色区域中向上,向右,向下,向左滑动。我现在如何获得例如向上滑动?或向下滑动?或向右滑动或向左滑动?例如如何获取一个字符串-> input = getSwiped(); ->输入然后是向上,或向右,向下,或向左 用户可以触摸两个按钮。1或2。1代表鸭子,2代表跳跃。 我想同时检

  • 我一直在youtube上学习android的自定义视图等等。在我尝试做一个表面视图(相当简单的描述在这个视频)。 我所做的事情与视频中所显示的几乎是同步的。

  • 本文向大家介绍vim 向上滚动,包括了vim 向上滚动的使用技巧和注意事项,需要的朋友参考一下 示例 命令 描述 Ctrl+Y 向上滚动一行。 Ctrl+U 向上滚动半个屏幕(可使用该scroll选项配置)。 Ctrl+B 向上滚动全屏。 z^ 在窗口底部的窗口上方绘制第一行。

  • 函数功能:向后滑动 函数方法 widget.scrollBackward(wid) 参数 类型 必填 说明 wid userdata 是 控件对象 函数用例 --脚本页面运行 --除控件函数外请勿将其他积木编程函数用于触动精灵,同理请勿将触动精灵函数用于积木编程 mSleep(1000) wid=widget.find({["id"]="com.play4u.luabox:id/item_rl_t

  • 当我做快速挺举时,我有一个CollapsingToolbarLayout的问题。隐藏appBar也会发生同样的事情 我需要工具栏隐藏在滚动和折叠工具栏折叠。当滚动不是很快时,它工作得很好。 但是当它很快的时候——似乎appBar在那之后就崩溃并扩展了。但它没有扩展回来。 请看视频。 折叠工具栏布局 应用程序栏 似乎这个答案是我需要的,但它不起作用。 我的折叠工具栏布局的 xml :

  • 问题内容: 我所看到的和jQuery的。左右滑动的功能/方式如何? 问题答案: 您可以使用jQueryUI中的其他效果来执行此操作: 快速示例: