Xamarin - RecyclerView with PagerSlidingTabStrip - System.NullReferenceException in SetLayoutManager()

Xamarin - RecyclerView with PagerSlidingTabStrip - System.NullReferenceException in SetLayoutManager()

我尝试在 SlideMenu 中使用 RecyclerView。

我将此示例用于我的 SlideMenu:https://github.com/jamesmontemagno/PagerSlidingTabStrip-for-Xamarin.Android

现在,我添加了带有 SlideMenu 的 RecyclerView。我需要两个菜单:"menu" 和 "product",所以我为我的片段使用了两个 xml 文件:menu.xml 用于 "menu" 和 menu_recyclerView.xml 用于 "product".

这是我的代码:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true" >
  <include
    android:id="@+id/top_menu"
    layout="@layout/top_menu"/>
  <com.refractored.PagerSlidingTabStrip
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:pstsShouldExpand="true"
      android:background="@color/Blue"
      app:pstsDividerWidth="1dp"
      app:pstsDividerPadding="12dp"
      app:pstsDividerColor="#50FFFFFF"
      android:textColor="@color/Green"
      app:pstsTextColorSelected="@color/White"
      app:pstsIndicatorColor="@color/Red"
      app:pstsUnderlineColor="@color/White"/>
  <android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

menu_recyclerView.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <android.support.v7.widget.CardView
      android:id="@+id/menu_recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <android.support.v7.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
  </android.support.v7.widget.CardView>
</LinearLayout>

row_recyclerView 是我的物品的图案

row_recyclerView.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp">
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:paddingRight="15dp"
      android:paddingLeft="15dp"
      android:orientation="vertical">
    <refractored.controls.CircleImageView
        android:id="@+id/imageView"
        app:civ_border_width="2dp"
        app:civ_border_color="#000000"/>
  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="88"
      android:orientation="vertical"
      android:paddingRight="10dp"
      android:paddingLeft="10dp"
      android:paddingTop="7dp"
      android:paddingBottom="7dp">
    <TextView
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtName"
        android:textColor="#000"
        android:gravity="center_vertical"
        android:padding="2dp"
        android:textSize="18sp" />
    <TextView
        android:text="Brand"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtBrand"
        android:textColor="#000"
        android:gravity="center_vertical"
        android:padding="2dp"
        android:textSize="14sp"
        android:singleLine="true" />
    <TextView
        android:text="Description"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtDescription"
        android:textColor="#000"
        android:gravity="center_vertical"
        android:padding="2dp"
        android:textSize="16sp" />
  </LinearLayout>
</LinearLayout>

-

public class MainActivity : BaseActivity
{
    protected override int LayoutResource
    {
        get
        {
            return Resource.Layout.Main;
        }
    }

    private Android.Widget.Toolbar _topMenu;
    private Adapter _adapter;
    private ViewPager _pager;
    private PagerSlidingTabStrip _tabs;

    private RecyclerView _recyclerView;
    private LayoutManager _layoutManager;

    public string _tag = "MainActivity";

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        _recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
        _layoutManager = new LayoutManager(this, _recyclerView);

        _layoutManager.addItem("one", "two", "three");
        _layoutManager.addItem("one", "two", "three");

        _layoutManager.createLayoutManager();

        _adapter = new Adapter(SupportFragmentManager);
        _pager = FindViewById<ViewPager>(Resource.Id.pager);
        _tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);

        _pager.Adapter = _adapter;
        _tabs.SetViewPager(_pager);
        _topMenu = FindViewById<Android.Widget.Toolbar>(Resource.Id.top_menu);
        SetActionBar(_topMenu);
    }

-

class LayoutManager
{
    public RecyclerView _mRecyclerView;
    public RecyclerView.LayoutManager _mLayoutManager;
    public RecyclerView.Adapter _mAdapter;
    public ItemList<Item> _mItems;
    public Activity _main;
    public LayoutManager(Activity main, RecyclerView recyclerView)
    {
        _main = main;
        _mRecyclerView = recyclerView;
        _mItems = new ItemList<Item>();
    }
    public void createLayoutManager()
    {
        _mLayoutManager = new LinearLayoutManager(_main);
        _mRecyclerView.SetLayoutManager(_mLayoutManager);
        _mAdapter = new RecyclerAdapter(_mItems, _mRecyclerView);
        _mItems.Adapter = _mAdapter;
        _mRecyclerView.SetAdapter(_mAdapter);
    }
    public void addItem(string name, string brand, string description)
    {
        _mItems.Add(new Item()
        {
            Img = Resource.Drawable.Icon,
            Name = name,
            Brand = brand,
            Desciption = description
        });
    }
}

在这一行中(在我的 LayoutManager.cs 中):

_mRecyclerView.SetLayoutManager(_mLayoutManager);

我有这个错误:

System.NullReferenceException: Object reference not set to an instance of an object.

我不明白问题的价值是什么? 我忘记了什么? 我完全迷路了? 请..帮助!

谢谢你, 罗曼

根据您的代码:public class MainActivity : BaseActivity

protected override int LayoutResource
{
    get
    {
        return Resource.Layout.Main;
    }
}

我猜你的 BaseActivityBaseActivity.cs of PagerSlidingTabStrip sample 一样。

然后你实际上在这里设置你的Main.xml作为你MainActivity的内容视图,而在这个Main.xml中,没有id为[=的RecyclerView 18=],它在你的 menu_recyclerView.xml 中。虽然您在编码时可以找到 id recyclerView,但它不存在于内容视图中。

所以在您的 MainActivity 中,当您尝试使用 _recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView); 找到它时,当您 运行 您的代码时,这里的 _recyclerView 应该为空。然后,当您调用 _layoutManager = new LayoutManager(this, _recyclerView); 时,您当然会向此 LayoutManager 传递一个空值。您可以在此行插入一个断点以检查它是否为空:

_recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);

要解决您的问题,我认为您可能需要重新设计布局或框架。

感谢您的回答! 是的,我的 BaseActivity 与 BaseActivity.cs of PagerSlidingTabStrip sample.

相同

我听从了你的建议,是的,我的架构有问题。 我了解了 RecyclerView 的工作原理,并添加了以下内容:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true" >

  <include
    android:id="@+id/top_menu"
    layout="@layout/top_menu"/>

  <com.refractored.PagerSlidingTabStrip
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"

      app:pstsShouldExpand="true"
      android:background="@color/Blue"

      app:pstsDividerWidth="1dp"
      app:pstsDividerPadding="12dp"
      app:pstsDividerColor="#50FFFFFF"

      android:textColor="@color/Green"
      app:pstsTextColorSelected="@color/White"

      app:pstsIndicatorColor="@color/Red"
      app:pstsUnderlineColor="@color/White"/>

  <android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

  <include
    layout="@layout/recyclerView" />


</LinearLayout>

menu_recyclerView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp">
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:paddingRight="15dp"
      android:paddingLeft="15dp"
      android:orientation="vertical">
      <refractored.controls.CircleImageView
          android:id="@+id/imageView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:civ_border_width="2dp"
          app:civ_border_color="#000000"/>
  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="88"
      android:orientation="vertical"
      android:paddingRight="10dp"
      android:paddingLeft="10dp"
      android:paddingTop="7dp"
      android:paddingBottom="7dp">
      <TextView
          android:text="Name"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/txtName"
          android:textColor="#000"
          android:gravity="center_vertical"
          android:padding="2dp"
          android:textSize="18sp" />
      <TextView
          android:text="Brand"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/txtBrand"
          android:textColor="#000"
          android:gravity="center_vertical"
          android:padding="2dp"
          android:textSize="14sp"
          android:singleLine="true" />
      <TextView
          android:text="Description"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/txtDescription"
          android:textColor="#000"
          android:gravity="center_vertical"
          android:padding="2dp"
          android:textSize="16sp" />
  </LinearLayout>
</LinearLayout>

我添加了新的 xml 文件:recyclerView.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
  </LinearLayout>
</android.support.v7.widget.CardView>

现在我在 Main.xml 中调用 recyclerView.xmlrecyclerView.xml包含CardView,他包装RecyclerView。

我应该将我的 RecyclerView 放在唯一的 CardView 中,或者我应该用 cardView 包装我的 menu_recyclerView.xml

我想创建一个这样的菜单:

我有一个结果,但现在我觉得我的 menu_recyclerView.xml 有问题,因为我没有列表,只是 "name, brand, description"。屏幕截图:

我搜索这个,是另外一个主题...

我认为这个问题的关键词是:RecyclerView, CardView 最后,这是我的其他来源:

https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156

非常感谢,