【问题标题】:Xamarin - RecyclerView with PagerSlidingTabStrip - System.NullReferenceException in SetLayoutManager()Xamarin - 带有 PagerSlidingTabStrip 的 RecyclerView - SetLayoutManager() 中的 System.NullReferenceException
【发布时间】:2017-04-26 13:37:40
【问题描述】:

我尝试在 SlideMenu 中使用 RecyclerView。

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

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

这是我的代码:

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.

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

谢谢你, 罗曼

【问题讨论】:

    标签: xamarin android-recyclerview xamarin.android pagerslidingtabstrip


    【解决方案1】:

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

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

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

    那么你实际上在这里将你的Main.xml 设置为你的MainActivity 的内容视图,而在这个Main.xml 中,没有id 为recyclerViewRecyclerView,它在你的menu_recyclerView.xml 中。虽然您在编码时可以找到 id recyclerView,但它在内容视图中不存在。

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

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

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

    【讨论】:

      【解决方案2】:

      感谢您的回答! 是的,我的 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 有问题,因为我没有列表,只有“名称、品牌、描述”。截图:

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

      我认为这个问题的好关键字是: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

      非常感谢,

      【讨论】:

        猜你喜欢
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        • 2023-03-25
        • 1970-01-01
        相关资源
        最近更新 更多