Android Cardslib&x27;s CardRecyclerView始终为空

Android Cardslib&x27;s CardRecyclerView始终为空,android,android-layout,android-recyclerview,android-cardview,cardslib,Android,Android Layout,Android Recyclerview,Android Cardview,Cardslib,我正在使用cardslib库开发和实现示例 尽管我在创建cardryclererview对象时严格按照教程中的说明操作,但该对象始终显示为null 检查我的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.native_recyclerview_card_layout);

我正在使用
cardslib
库开发和实现示例

尽管我在创建
cardryclererview
对象时严格按照教程中的说明操作,但该对象始终显示为
null

检查我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.native_recyclerview_card_layout);

    ArrayList<Card> cards = new ArrayList<Card>();

    //Create a Card
    Card card = new Card(this);//getContext()

    //Create a CardHeader and Add Header to card
    CardHeader header = new CardHeader(this);
    card.addCardHeader(header);

    cards.add(card);

    CardArrayRecyclerViewAdapter mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this,cards);
    //Staggered grid view
    CardRecyclerView mRecyclerView = (CardRecyclerView) 
                   this.findViewById(R.id.carddemo_recyclerview); //**mRecyclerView null here**
    mRecyclerView.setHasFixedSize(false); //**NullPointerException here**
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    //Set the empty view
    if (mRecyclerView != null) {
        mRecyclerView.setAdapter(mCardArrayAdapter);
    }

    //Set card in the cardView
    CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
    cardView.setCard(card);
}
cardslib\u项目\u卡片\u视图

<?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:id="@+id/card_view"
    android:layout_width="match_parent" android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    android:layout_margin="5dp">

<RelativeLayout android:id="@+id/nativecardlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="?android:selectableItemBackground">        

  <!--some more layout-->

</RelativeLayout>
</android.support.v7.widget.CardView>


我不明白为什么即使我将
CardRecyclerView
对象引用到正确的xml
cardslib\u activity\u recycler.xml
,我最终还是发现它为空。我忽略了什么吗?

可能是因为您设置了
setContentView(R.layout.native\u recyclerview\u card\u layout)
但您的CardRecyclerView位于
cardslib\u activity\u recycler.xml


从技术上讲,
findViewById
仅查找在由
setContentView
设置的布局的视图层次结构中注册的id视图。如果要合并视图,则应使用

否,因为我正在使用
卡:列表\u卡\u布局\u资源\u Id设置新的资源Id=“@layout/native\u recyclerview\u card\u layout
因此,当我设置它时,它指的是'cardslib\u activity\u recycler.xml`@CrisBenois,我真的不知道该参数的作用,因为它是一个自定义视图。但是上面的
findViewById
仅在
setContentView
中查找在布局的视图层次结构中注册的id。如果你想合并视图,那么你应该使用
。我正在努力,但你是对的。。。也许你应该考虑编辑你的答案。如果行得通,我就接受;)
<?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:id="@+id/card_view"
    android:layout_width="match_parent" android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    android:layout_margin="5dp">

<RelativeLayout android:id="@+id/nativecardlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="?android:selectableItemBackground">        

  <!--some more layout-->

</RelativeLayout>
</android.support.v7.widget.CardView>