1

I am developing with cardslib library and implementing this example.

Although I slavishly follow the instructions in the tutorial when I create a CardRecyclerView object, that object appears to always be null.

Check my code:

@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>

I don't see why even though I am referencing the CardRecyclerView object to the correct xml cardslib_activity_recycler.xml, I eventually find it to be null. Did I overlook something?

Cris
  • 2,002
  • 4
  • 30
  • 51

1 Answers1

1

Maybe because you set setContentView(R.layout.native_recyclerview_card_layout) but your CardRecyclerView is in cardslib_activity_recycler.xml ?

Technically findViewById only finds view by id registered in the view hierarchy of the layout set by setContentView. If you want to combine views then you should use <include>.

inmyth
  • 8,880
  • 4
  • 47
  • 52
  • No because I am setting the new resource Id with `card:list_card_layout_resource_ID="@layout/native_recyclerview_card_layout ` and therefore when I set it it refers to ` cardslib_activity_recycler.xml ` – Cris Apr 29 '15 at 19:56
  • 1
    @CrisBenois I really don't know what that parameter does because it is a custom view. But `findViewById` above only finds the id registered in the view hierarchy of the layout in `setContentView`. If you want to combine views then you should use ``. – inmyth Apr 30 '15 at 04:16
  • I am working on it, but you're right... probably you should consider editing your answer. If it works I'll accept it ;) – Cris Apr 30 '15 at 09:53