Xamarin - 带有PagerSlidingTabStrip的RecyclerView - SetLayoutManager中的System.NullReferenceException()

时间:2017-04-26 13:37:41

标签: xamarin android-recyclerview xamarin.android pagerslidingtabstrip

我尝试在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.

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

谢谢你, 罗曼

2 个答案:

答案 0 :(得分:0)

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

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

我猜你的BaseActivityBaseActivity.cs of PagerSlidingTabStrip sample相同。

然后,您实际上将Main.xml设置为MainActivity的内容视图,而在此Main.xml中,没有RecyclerView的ID为recyclerView },它在你的menu_recyclerView.xml。虽然您可以在编码时找到ID recyclerView,但内容视图中不存在该ID。

因此,在MainActivity中,当您尝试使用_recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);找到它时,此_recyclerView在您运行代码时应为null。当然,当您致电_layoutManager = new LayoutManager(this, _recyclerView);时,您会将空值传递给此LayoutManager。您可以在此行上插入断点以检查它是否为空:

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

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

答案 1 :(得分:0)

感谢您的回答! 是的,我的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>

现在我在recyclerView.xml中拨打Main.xmlrecyclerView.xml包含CardView,并且包装了RecyclerView。

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

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

enter image description here

我有结果,但现在我觉得我的menu_recyclerView.xml有问题,因为我没有列表,juste“名称,品牌,描述”。截图:

enter image description here

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

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

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

How to add listView inside cardView android?

How to implement RecyclerView with CardView rows in a Fragment with TabLayout

非常感谢,