Chien Chien - 2 months ago 17
Android Question

Resources$NotFoundException: Resource ID #0x0 in setViewBinder setViewValue

I have FATAL EXCEPTION Resources$NotFoundException when set image for Imageview in listview in fragment.

My table( _id integer PRIMARY KEY autoincrement, name text,image text) //image store filename

My data

public void insertsomeCatalog(){
add("Salary Money","R.drawable.salary");
add("Saving Money","R.drawable.saving");
add("Other Money","R.drawable.other");
}


My FragmentActivity

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment, container, false);
view = inflater.inflate(R.layout.fragment_thu,
container, false);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
listView = (ListView) view.findViewById(R.id.lv_thu);
dbHelper=new MyDatabaseHelper(view.getContext());
dbHelper.open();

Cursor cursor = dbHelper.getAll();


String[] columns = new String[] {
"name",
"image"
};

int[] to = new int[] {
R.id.tvname,
R.id.imageView,

};

dataAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.list_item_layout,cursor, columns,to, 0);
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue (View view, Cursor cursor, int columnIndex){
if (view.getId() == R.id.imageView) {
ImageView IV=(ImageView) view;
int resID = getActivity().getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable", getActivity().getApplicationContext().getPackageName());
IV.setImageDrawable(getActivity().getApplicationContext().getResources().getDrawable(resID));
return true;
}
return false;
}
});

listView.setAdapter(dataAdapter);
return view;
}


I have add three image salary.png, saving.png and other.png into foder drawable

Logcatt

09-28 20:45:54.692 27457-27457/com.example.lananh.da_quanlychitieu E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1118)
at android.content.res.Resources.getDrawable(Resources.java:693)
at com.example.lananh.da_quanlychitieu.ThuFragment$1.setViewValue(ThuFragment.java:144)
at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:146)
at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
at android.widget.ListView.onMeasure(ListView.java:1159)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:765)
at android.view.View.measure(View.java:15848)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)


Update

I show values resId
Log.i("ResID: ", String.valueOf(resID));


09-28 23:21:38.744 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.744 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.748 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.764 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.764 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.768 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.884 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.888 9172-9172/doan.anhchien.myapplication I/ResID:: 0
09-28 23:21:38.900 9172-9172/doan.anhchien.myapplication I/ResID:: 0

Answer

In insertsomeCatalog provide drawable name. eg: change "R.drawable.salary" to "salary"

Comments