NullPointerException on CursorAdapter bindView method
-
27-10-2019 - |
문제
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.
해결책
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);
다른 팁
I was having a similar problem where bindView() was throwing a NullPointerException. I solved it by calling setEmptyView()
on my ListView.