gpt4 book ai didi

android - Cardslib 的 CardRecyclerView 始终为 null

转载 作者:行者123 更新时间:2023-11-30 02:12:05 28 4
gpt4 key购买 nike

我正在使用 cardslib 库进行开发并实现 this示例。

尽管我在创建 CardRecyclerView 对象时严格按照教程中的说明进行操作,但该对象似乎始终为 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_activity_recycler.xml:

<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
card:list_card_layout_resourceID="@layout/native_recyclerview_card_layout"
android:id="@+id/carddemo_recyclerview"/>

cardslib_item_card_view:

<?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_activity_recycler.xml,我最终还是发现它为空。我是不是忽略了什么?

最佳答案

可能是因为你设置了setContentView(R.layout.native_recyclerview_card_layout)但是你的 CardRecyclerView 在 cardslib_activity_recycler.xml 中?

技术上 findViewById仅查找在由 setContentView 设置的布局的 View 层次结构中注册的 ID 的 View .如果你想合并 View ,那么你应该使用 <include> .

关于android - Cardslib 的 CardRecyclerView 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949698/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com