Android : Simplest layout thing driving me crazy

on Sunday, April 19, 2015


I've a TextView inside a CardView. Now the problem is when I use setText method on the TextView it's giving me nullPointerException. According to my tests textViewAuthor in the below code is null even after initializing it. Weird part is textViewName which is initialized the same way is not null. Why is it staying null even after assigning a view?



...
textViewName = (TextView)v.findViewById(R.id.textViewName); //not null
textViewAuthor = (TextView)v.findViewById(R.id.textViewAuthor); //NULL !!

if(textViewName == null)
Log.e("textViewName","null");
if(textViewAuthor == null)
Log.e("textViewAuthor","null");

Log.i("NAME", ebookName[position]); //not null
Log.i("AUTHOR", ebookAuthor[position]); //not null

holder.textViewName.setText(ebookName[position]);
holder.textViewAuthor.setText(ebookAuthor[position]);
...


Here's my layout code:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:layout_weight="0.25">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp">

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_margin="2dp" />

<TextView
android:id="@+id/textViewAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="2dp" />

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

</LinearLayout>

0 comments:

Post a Comment