【问题标题】:Cardslib's CardRecyclerView is always nullCardslib 的 CardRecyclerView 始终为空
【发布时间】:2015-04-29 16:40:15
【问题描述】:

我正在使用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,我最终还是发现它为空。我是不是忽略了什么?

【问题讨论】:

    标签: android android-layout android-recyclerview android-cardview cardslib


    【解决方案1】:

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

    从技术上讲,findViewById 只能通过setContentView 设置的布局的视图层次结构中注册的 id 查找视图。如果你想合并视图,那么你应该使用&lt;include&gt;

    【讨论】:

    • 否,因为我使用card:list_card_layout_resource_ID="@layout/native_recyclerview_card_layout 设置新资源ID,因此当我设置它时,它指的是`cardslib_activity_recycler.xml`
    • @CrisBenois 我真的不知道该参数的作用,因为它是自定义视图。但是上面的findViewById只能找到setContentView中布局的视图层次中注册的id。如果你想合并视图,那么你应该使用&lt;include&gt;
    • 我正在努力,但你是对的......也许你应该考虑编辑你的答案。如果它有效,我会接受它;)
    猜你喜欢
    • 1970-01-01
    • 2017-07-31
    • 2019-07-18
    • 2019-08-18
    • 2011-12-29
    • 2019-07-02
    • 2019-05-04
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多