Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException on CursorAdapter bindView method

I created a class extending CursorAdapter, i am having problem on the bindView method.

@Override
public void bindView(View v, Context context, Cursor c) {


    int colNum = c.getColumnIndex(VidCallDbAdapter.QRLINK);
    String colValue = c.getString(colNum);

    System.out.println("value>> " + colValue);

    TextView name_text = (TextView) v.findViewById(R.id.qr_url);

    name_text.setText(colValue);

}

im always getting a NullPointerException in this line

TextView name_text = (TextView) v.findViewById(R.id.qr_url);

It's weird because i defined qr_url on one of my xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

<TextView android:layout_height="wrap_content" 
          android:text=""   
          android:id="@+id/qr_url" 
          android:gravity="center_vertical" 
          android:layout_width="fill_parent" 
          android:textColor="#000000" 
          android:textSize="12sp">
</TextView>

</LinearLayout>  

Did i miss something on the code that's why it's a NullPointerException? Thanks in advance.

like image 281
androidnewbie Avatar asked Aug 01 '26 06:08

androidnewbie


1 Answers

If you are getting NullPointerException in this line

TextView name_text = (TextView) v.findViewById(R.id.qr_url);

it means that v is null, cause if TextView is not found null would be returned and NullPointerException would be thrown in

name_text.setText(colValue);

So make sure your public View newView(...) is overrided correctly.

edit what you should return in newView is

return inflater.inflate(R.layout.<name of your xml>, parent, false);
like image 141
Vladimir Avatar answered Aug 03 '26 23:08

Vladimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!