列表视图显示滚动自定义适配器

时间:2017-09-09 18:11:00

标签: android listview

我正在使用自定义适配器来显示列表视图但不幸的是,当我滚动它时显示空对象引用。

我已经查看了之前提出的问题,但没有任何建议吗?

截图:

![在此处输入图片说明] [1]

09-09 13:39:53.949 3111-3111 / com.aug22.avinashchintareddy.ecommerce E / AndroidRuntime:FATAL EXCEPTION:main                                                                                       流程:com.aug22.avinashchintareddy.ecommerce,PID:3111                                                                                       java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在null对象引用上                                                                                           在com.aug22.avinashchintareddy.ecommerce.adapters.ProductAdapt.getView(ProductAdapt.java:79)                                                                                           在android.widget.AbsListView.obtainView(AbsListView.java:2346)                                                                                           在android.widget.ListView.makeAndAddView(ListView.java:1875)                                                                                           在android.widget.ListView.fillDown(ListView.java:702)                                                                                           在android.widget.ListView.fillGap(ListView.java:666)                                                                                           在android.widget.AbsListView.trackMotionScroll(AbsListView.java:5029)                                                                                           在android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3424)                                                                                           在android.widget.AbsListView.onTouchMove(AbsListView.java:3820)                                                                                           在android.widget.AbsListView.onTouchEvent(AbsListView.java:3651)                                                                                           在android.view.View.dispatchTouchEvent(View.java:9294)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)                                                                                           在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                           在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)

这是错误日志

我的适配器类     在这里输入代码

在侧面获取视图中添加日志以检查发生了什么但不幸的是,当列表滚动时,渲染元素被显示但不是所有元素 您还可以在获取视图大小时看到添加的日志。 适配器类。


         public class ProductAdapt
        extends BaseAdapter {
        List 
        productAcessorList ;
        Context context;
         LayoutInflater layoutInflater;
         setClicker clicker;

          public ProductAdapt(List
                 productAcessorList, Context context) {
            this.productAcessorList = productAcessorList;
            this.context = context;
            layoutInflater=(LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public int getCount() {

            Log.i("List size",""+
            productAcessorList.size());
            return productAcessorList.size();
                }

            @Override
            public Object 
            getItem(int i) {
             return i;
                }

         @Override
         public long getItemId(int i) {
           return i;
             }

            public static class Handler{
            TextView tx_prodname,
            tx_proddesc,tx_proid;
            ImageView img_prd;

            }

          @Override
            public View getView(final int i, 
            View myview, ViewGroup viewGroup) {
            Handler handler = new Handler();
            if(myview == null){
              myview=layoutInflater.inflate
              (R.layout.list_prod,viewGroup,false);
              handler.tx_prodname=(TextView) 
              myview.findViewById(R.id.txt_categid);
              handler.tx_proddesc=(TextView) 
              myview.findViewById(R.id.txt_descid);
              handler.tx_proid=(TextView) 
              myview.findViewById(R.id.txt_idi);
              handler.img_prd=myview.
              findViewById(R.id.img_view);

                myview.setTag(handler);
             }
            else
            {
              myview.getTag();

            }

            Log.i("find fault",""+
            productAcessorList.get(i).getProduct());

          handler.tx_prodname.
          setText(""+productAcessorList.get(i).getProduct());
          handler.tx_proddesc.
          setText(""+productAcessorList.get(i).getDescrip());
          handler.tx_proid.
          setText(""+productAcessorList.get(i).getId());
          Picasso.with(context).
          load(""+productAcessorList.
          get(i).getImage()).
          into(handler.img_prd);
           myview.
           setOnClickListener
           (new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if(clicker!=null){
                    clicker.gotClick(view,i);

                }

                }
             });

            return myview;
         }


         public void getClicker
            (setClicker clicker){
          this.clicker=clicker;

            }

            }



1 个答案:

答案 0 :(得分:0)

而不是

myview.getTag();
你应该

handler = (Handler) myview.getTag();

顺便说一句,这类通常称为HolderViewHolder。它可以避免在每次getView调用中重复执行findViewById()调用等耗时的任务。

对于每个新项目视图,您都可以创建一个视图持有者并填充它。然后将其设置为视图标记。没关系。这一切都是为了以后能够获取此标记并将其用作视图持有者。但是在您的代码中,您从未使用过标记,而是使用空字段访问空的holder实例。