フラグメントビューのImageviewにimageを設定すると、致命的な例外リソース$ 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;
}
fender drawableにsalary.png、saving.png、other.pngの3つのイメージを追加する
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)
更新
私はinsertsomeCatalog
で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
に変更
"R.drawable.salary"
私はこの問題の行で 'int型RESID = getActivity()getApplicationContext()getResources()getIdentifier(cursor.getString(columnIndexに)、 "描画可能" と推測している、getActivity(。。。 ).getApplicationContext()。getPackageName()); '行143を呼び出し、次の行でブレークします。 '? – Sharkはい、そうだと思いますが、修正できません。 – Chien