0

SlideMenuでRecyclerViewを使用しようとしています。Xamarin - RecyclerViewとPagerSlidingTabStrip - System.NullReferenceException(SetLayoutManager())

私はSlideMenuために、この例を使用します。https://github.com/jamesmontemagno/PagerSlidingTabStrip-for-Xamarin.Android

そして今、私はSlideMenuでRecyclerViewを追加します。私は2つのメニューが必要です: "メニュー"と "製品"、私は断片のための2つの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で)この行に:

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

_mRecyclerView.SetLayoutManager(_mLayoutManager); 

私はこのエラーを持っています

問題の価値は何か分かりません。 私は何かを忘れていますか? 私は完全に失われていますか? お願いします!

おかげで、あなたを、あなたのコードによると ロマン

答えて

0

public class MainActivity : BaseActivity

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

私はあなたのBaseActivityBaseActivity.cs of PagerSlidingTabStrip sampleと同じであることを推測します。

は、その後、あなたが実際にここにあなたのMainActivityのコンテンツビューとしてあなたMain.xmlを設定し、このMain.xmlで、何RecyclerViewにid recyclerViewではありません、それはあなたのmenu_recyclerView.xmlです。コーディング時にはID recyclerViewが見つかりますが、コンテンツビューには表示されません。

MainActivityでは、_recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);を使用して検索しようとすると、コードを実行するときにこの_recyclerViewがnullになるはずです。もちろん、_layoutManager = new LayoutManager(this, _recyclerView);に電話すると、LayoutManagerにはnullが渡されます。あなたはそれがnullであるかどうかを確認するために、この行にブレークポイントを挿入することができます。

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

あなたの問題を解決するために、私はあなたがあなたのレイアウトや、あなたのフレームを再設計する必要があるかもしれないと思います。

0

ありがとうございました! はい私のBaseActivityは同じBaseActivity.cs of PagerSlidingTabStrip sample.

私はあなたの助言に従う、はい私のアーキテクチャには問題があります。 は、私はそれがRecyclerViewどのように動作するかを学び、そして私は、この追加: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"/> 

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


</LinearLayout> 

menu_recyclerView.xmlを私は新しい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 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を呼び出します。 recyclerView.xmlにはCardViewがあり、彼はRecyclerViewをラップします。

私はRecyclerViewを唯一のCardViewに入れるべきですか、私はmenu_recyclerView.xmlをcardViewで囲むべきですか?

私はこのようなメニューを作成したい:私は結果を持っていますが、今、私は私がリストを持っていないので、私は私のmenu_recyclerView.xmlとproblemeを持っていると思う、ジュスト「名前

enter image description here

を、ブランド、説明 "。スクリーンショット:

enter image description here

私はこれを検索し、別の主題である...

私はこのproblemeための良いキーワードがあると思う: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

ありがとう、

関連する問題