我在片段列表视图中为Imageview设置图像时有FATAL EXCEPTION资源$ NotFoundException。
我的表格(_id整数PRIMARY KEY自动增量,名称文字,图像文字)//图像存储文件名
我的数据
public void insertsomeCatalog(){
add("Salary Money","R.drawable.salary");
add("Saving Money","R.drawable.saving");
add("Other Money","R.drawable.other");
}
我的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;
}
我已将三张图片salary.png,saving.png和other.png添加到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)
更新
我显示值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
答案 0 :(得分:1)
在insertsomeCatalog
中提供可绘制的名称。
例如:将"R.drawable.salary"
更改为"salary"