RecyclerView尝试在空对象引用上复制虚拟方法getItemCount()[重复]

问题描述 投票:-1回答:2

我正在尝试在一个片段中使用recyclerview,我在一个项目中使用了recyclerview,它在一个活动中效果很好,直到我尝试在另一个片段项目中实现它。我试图将两者结合起来,但出现这些错误:

LinearLayoutManager.getItemCount()上的NullPointerException

什么可能导致NullPointerException?

Process: com.example.accueil, PID: 23897
    java.lang.NullPointerException: Attempt to invoke virtual method 'int androidx.recyclerview.widget.LinearLayoutManager.getItemCount()' on a null object reference
        at com.example.accueil.Adapter.MyAdapter$1.onScrolled(MyAdapter.java:72)
        at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4961)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4021)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3652)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
        at android.view.View.layout(View.java:16022)
        at android.view.ViewGroup.layout(ViewGroup.java:5181)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2483)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2180)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
        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:5631)
        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)

这是我的代码:

MyAdapter.java:

class LoadingViewHolder extends RecyclerView.ViewHolder

{
    public ProgressBar progressBar;
    public LoadingViewHolder( View itemView) {
        super(itemView);
        progressBar= (ProgressBar) itemView.findViewById(R.id.progressBar);

    }
}

class ItemViewHolder extends RecyclerView.ViewHolder{

    public TextView title,pub_date;
    public ImageView thumbnail;

    public ItemViewHolder( View itemView) {
        super(itemView);
        title=(TextView) itemView.findViewById(R.id.txtName);
        pub_date=(TextView) itemView.findViewById(R.id.txtLength);
        thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
    }
}

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private final int VIEW_TYPE_ITEM=0,VIEW_TYPE_LOADING=1;
IloadMore loadMore;
boolean isLoading;
Activity activity;
List<Item> items;
int visibleThreshold=5;
int LastVisibleItem,totalItemCount;
RequestOptions option;

    public MyAdapter(RecyclerView recyclerView,Activity activity, List<Item> items) {
        this.activity = activity;
        this.items = items;

        // Request option for Glide
        option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);

        final LinearLayoutManager linearLayoutManager=(LinearLayoutManager) recyclerView.getLayoutManager();
   recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
       @Override
       public void onScrolled( RecyclerView recyclerView, int dx, int dy) {
           super.onScrolled(recyclerView, dx, dy);
           totalItemCount= linearLayoutManager.getItemCount();
           LastVisibleItem=linearLayoutManager.findLastVisibleItemPosition();
           if (!isLoading && totalItemCount <= (LastVisibleItem + visibleThreshold) )
           {
               if(loadMore!=null) {
                   loadMore.onLoadMore();
                   isLoading = true;
               }
           }

       }
   });

    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position)==null ?VIEW_TYPE_LOADING:VIEW_TYPE_ITEM;
    }

    public void setLoadMore(IloadMore loadMore) {
        this.loadMore = loadMore;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

      if(viewType == VIEW_TYPE_ITEM)
      {
          View view =LayoutInflater.from(parent.getContext())
              .inflate(R.layout.item_layout,parent,false);
          return new ItemViewHolder(view);


      }
      else if(viewType==VIEW_TYPE_LOADING )
      {
          View view =LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.item_loading,parent,false);
          return new LoadingViewHolder(view);

      }
        return null;


    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder)
{
    Item item= items.get(position);
    ItemViewHolder viewHolder=(ItemViewHolder) holder;
     viewHolder.title.setText(items.get(position).getTitle());
     viewHolder.pub_date.setText(items.get(position).getDatePub());


    /*if(items.get(position).getImage() != null)
        Picasso.get().load(items.get(position).getImage()).into(viewHolder.thumbnail);
    else*/
        Picasso.get().load("https://www.victimeslactalis.fr/wp-content/uploads/2019/01/POURQUOI_DOCTEUR.png").into(viewHolder.thumbnail);


}
else if(holder instanceof LoadingViewHolder)
{
        LoadingViewHolder loadingViewHolder =(LoadingViewHolder) holder;
        loadingViewHolder.progressBar.setIndeterminate(true);

}
    }

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

    public void setLoaded() {
        isLoading = false;
    }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {


    private TabLayout tablayout;
    private ViewPager viewPager;
    private TabItem tab1 , tab2, tab3, tab4, tab5,tab6,tab7;
    public PagerAdapter pagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tablayout = (TabLayout) findViewById(R.id.tablayout);
        tab1 = (TabItem) findViewById(R.id.Tab1);
        tab2 = (TabItem) findViewById(R.id.Tab2);
        tab3 = (TabItem) findViewById(R.id.Tab3);
        tab4 = (TabItem) findViewById(R.id.Tab4);
        tab5 = (TabItem) findViewById(R.id.Tab5);
        tab6 = (TabItem) findViewById(R.id.Tab6);
        tab7 = (TabItem) findViewById(R.id.Tab7);
        viewPager = findViewById(R.id.viewpager);

        pagerAdapter = new PageAdapter(getSupportFragmentManager(), tablayout.getTabCount());
       viewPager.setAdapter(pagerAdapter);
       tablayout.setOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
           @Override
           public void onTabSelected(TabLayout.Tab tab) {
               viewPager.setCurrentItem(tab.getPosition());
               if (tab.getPosition() == 0) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 1) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 2) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 3) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 4) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 5) {
                   pagerAdapter.notifyDataSetChanged();
               } else if (tab.getPosition() == 6) {
                   pagerAdapter.notifyDataSetChanged();
               }
           }

           @Override
           public void onTabUnselected(TabLayout.Tab tab) {

           }

           @Override
           public void onTabReselected(TabLayout.Tab tab) {

           }
       });



       viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
    }}

Tab1.java:

public class tab1 extends Fragment {

    List<Item> items= new ArrayList<>();
    MyAdapter adapter;
    int number, total;


    public tab1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);

        RecyclerView recycler=(RecyclerView)view.findViewById(R.id.recycler);


        //new GetFirstData().execute();
        random10Data();

        adapter = new MyAdapter(recycler,getActivity(),items);
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycler.setAdapter(adapter);


        return view;
    }


    private void random10Data() {
        for (int i=0; i<10;i++)
        {
            String title = "Static";
            String pub_date = "0000";
            Item item= new Item("",title,"",pub_date, "");
            items.add(item);

        }
        //new GetData().execute();
    }
}

fragment_tab1.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".tab1">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
        <include
            layout="@layout/home"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawermenu" />

    </androidx.drawerlayout.widget.DrawerLayout>

item_layout.xml:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF"
    android:orientation="horizontal"
    >




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:layout_margin="8dp">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:adjustViewBounds="true"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:textStyle="bold"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/txtLength"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawablePadding="5dp"
                android:textColor="@android:color/black" />


        </LinearLayout>




    </LinearLayout>

很抱歉,代码冗长而混乱,但是你们能告诉我我在做什么错吗?

java android android-fragments android-recyclerview tabs
2个回答
0
投票

主要问题是您的数据没有正确发送到适配器,请确保您的项目数据正确发送到适配器。您可以使用以下代码:

int getItemCount(){
return  ItemList == null ? 0 : ItemList.size();
}

0
投票

主要问题在您的onCreateView片段中:

    adapter = new MyAdapter(recycler,getActivity(),items);
    recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

由于调用了MyAdapter构造函数,并且由于要在其参数中传递recyclerview,因此recyclerView并未设置布局管理器,因此,

totalItemCount= linearLayoutManager.getItemCount();

抛出一个NullPointer异常,因为linearLayoutManager为null。

根据您的情况进行修复的最简单方法是在创建适配器(如下所示之前,先设置一个布局管理器,只需将其互换即可解决您的问题。

recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new MyAdapter(recycler,getActivity(),items);

但是总的来说,我认为可能还需要其他代码优化。让我知道是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.