RecyclerViewAdapter的onBindViewHolder()方法

时间:2016-02-01 10:29:58

标签: android android-adapter android-recyclerview

你可能看起来和其他NullPionterException问题类似,在将它标记为重复之前,请先阅读并尝试先了解我的问题并指出我错在哪里。

实际上我的应用程序正在访问一些JOSN数据,然后解析它并创建一个POJO对象并将该对象放入某个ArrayList<SchemesHitsPojoModel> schemesListHitsWise然后我将此ArrayList发送到我的RecyclerViewAdapter类,然后将其显示在RecyclerView中。

但在我的onBindViewHolder方法中,当我尝试在schemeName上设置一些文本时,它会给我以下错误。帮助我们,我想在最近几天解决这个问题并阅读大量答案,但仍然存在这个错误。

logcat的

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:71)
            at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:21)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at c

MyAdapter文件

 @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        MyViewHolder viewHolder;
        switch (success){
            case(0):
                view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                break;
            case(1):
                view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);

        }
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
        success = firstObject.getSuccess();
        if(success == 0 ){
            Log.d("SORRY!, "," We don't have any records to show you.");
        }
        else{
            SchemesHitsPojoModel currentSchemeRow =  schemesListHitsWise.get(position);
            holder.schemeName.setText(currentSchemeRow.getSchemeName());
            int langId = currentSchemeRow.getLanguage_id();
            switch(langId){
                case(1):
                    holder.languageName.setText("Punjabi");
                    break;
                case(2):
                    holder.languageName.setText("English");
            }

            holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
            holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
            holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
            //holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
        }
    }

// MyViewHolder
    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView schemeName, schemeViewDate, languageName, schemeHits,totalHitsOnThisScheme;
        ImageView viewIcon;

        public MyViewHolder(View itemView) {
            super(itemView);
            schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);
            languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);
            viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);
            schemeViewDate = (TextView) itemView.findViewById(R.id.hitsOnSchemeViewDate);
            schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);
            //totalHitsOnThisScheme = (TextView) itemView.findViewById(R.id.totalHitsOnThisScheme);
        }
    }

RecyclerView的自定义行的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">


    <GridLayout
        android:id="@+id/gridLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/hitsOnSchemeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:width="270dp"
            android:text="Name of the Scheme"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/hitsOnViewIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            android:src="@drawable/ic_remove_red_eye_black_18dp" />

        <TextView
            android:id="@+id/hitsOnSchemeHits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_gravity="left|center"
            android:layout_marginLeft="3dp"
            android:layout_row="0"
            android:text="5"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/hitsOnLangName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="1"
            android:width="270dp"
            android:text="Language of scheme"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/hitsOnSchemeViewDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_row="1"
            android:text="1989-12-09"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12dp" />
    </GridLayout>

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

我认为你的问题在这里

@Test

将此更改为

holder.schemeName.setText(currentSchemeRow.getSchemeName()); );

移动

holder.schemeName.setText(currentSchemeRow.get(position).getSchemeName()

out of else方法

答案 1 :(得分:0)

你需要改变,

有两次机会获得此错误。

1.改变你的

holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));

   holder.schemeViewDate.setText(currentSchemeRow.get(position).getSchemeViewDate());
   holder.schemeHits.setText("" + currentSchemeRow..get(position).getSchemeHits());

currentSchemeRow应该是您获取和设置数据的模型的Arraylist,,您必须从获取(位置)中获取您所选位置的值。< / p>

  1. 可能使用了错误的ID或未在两个布局文件中声明任何内容 custom_recyclerview_row_each_scheme_hits custom_no_record_found_msg_screen
  2. schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);    
     languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);    
     viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);    
     schemeViewDate=(TextView)itemView.findViewById(R.id.hitsOnSchemeViewDate); 
     schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);
    

答案 2 :(得分:0)

你的问题出现在onCreateViewHolder

    switch (success){
        case(0):
            view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
            break;
        case(1):
            view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);

这里你膨胀了两种不同的布局,所以你的所有TexView都必须存在于两种布局中。

OR

在onBindViewHolder中放置一个if条件来处理空指针异常,因为在这两种情况下都必须调用onBindViewHolder。

答案 3 :(得分:0)

首先谢谢你们,我非常感谢你的帮助。实际上这是我的坏事。

我将success变量中的值存储在onBindViewHolder()内部,该变量将用于两个方法(onCreateViewHolder(),onBindViewHolder())中,这是我的实际错误

因为我忘记了我在success中使用此onCreateViewHolder()变量以及针对特定情况选择我的特定布局,并且因为我onCtreatViewHolder()运行时的错误,时间success=0并返回错误的布局。

现在底线就是这个,

  1. 我创建名为success

  2. 的全局变量
  3. onCreateViewHolder()内部方法i存储success的值

  4. 然后,我将这个success变量用于我的两种方法

  5. 这是我目前正常运作的解决方案。

     private int success;
    
        // Constructor
        public HitsOnSchemesDataDisplayAdapter(Context context){
            layoutInflater = LayoutInflater.from(context);
        }
    
        public void setSchemesListHitsWise(ArrayList<SchemesHitsPojoModel> schemesListHitsWise){
            this.schemesListHitsWise = schemesListHitsWise;
            notifyItemRangeChanged(0, schemesListHitsWise.size());
        }
    

    onCreateViewHolder()方法

    @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = null;
            MyViewHolder viewHolder;
            SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
            success = firstObject.getSuccess();
            switch (success){
                case(0):
                    view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                    Log.d("VIEW "," is runned....");
                    break;
                case(1):
                    view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);
                    Log.d("VIEW_2, "," is run....");
                    break;
    
                default:
                    view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                    break;
            }
            viewHolder = new MyViewHolder(view);
            return viewHolder;
        }
    

    onBindViewHolder()方法

     @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
            //int success = firstObject.getSuccess();
            if(success == 0 ){
                Log.d("SORRY!, "," We don't have any records to show you.");
            }
            else if(success == 1){
                SchemesHitsPojoModel currentSchemeRow =  schemesListHitsWise.get(position);
                holder.schemeName.setText(currentSchemeRow.getSchemeName());
                int langId = currentSchemeRow.getLanguage_id();
                switch(langId){
                    case(1):
                        holder.languageName.setText("Punjabi");
                        break;
                    case(2):
                        holder.languageName.setText("English");
                }
    
                holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
                holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
                holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
                //holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
            }
        }