我正在将带有扩展名的图像名称存储在sqlite示例(“ food.xml”)中。
当我获取图像名称并将其设置为ImageView时,出现此错误。
我的适配器类
String uri = "@drawable/" + expenseCategory.getCategory_image();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
cat_img.setImageDrawable(res);
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp">
<LinearLayout
android:id="@+id/image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/item_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circleyellow"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/food" />
</LinearLayout>
<TextView
android:id="@+id/item_cat_text"
android:layout_width="50dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_below="@+id/image_layout"
android:text="Food"
android:textColor="#000"
android:textSize="12sp" />
</RelativeLayout>
答案 0 :(得分:2)
您不应该包含{label, value, id}
,因此假设2018-11-05 10:21:27.522 ERROR 3132 --- [io-8443-exec-11] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.neo4j.ogm.exception.CypherException: Error executing Cypher "Neo.DatabaseError.Statement.ExecutionFailed"; Code: Neo.DatabaseError.Statement.ExecutionFailed; Description: Reference value not initialised at offset 0 in
PrimitiveExecutionContext {
SlotConfiguration(longs=3, refs=1, slots=Map( label@1528 -> RefSlot(0,true,Any), b@1458 -> LongSlot(1,false,Relationship), a@1448 -> LongSlot(2,false,Node), c@1462 -> LongSlot(0,false,Node)))
LongSlot(0,false,Node) ' c@1462' = Long(1771)
LongSlot(1,false,Relationship) ' b@1458' = Long(5286)
LongSlot(2,false,Node) ' a@1448' = Long(1770)
RefSlot(0,true,Any) ' label@1528' = null
}
] with root cause
将返回可绘制对象中存在的文件名,那么这应该起作用:
@drawable/
答案 1 :(得分:1)
从URI变量中删除扩展名。您应该让uri看起来像@ drawable / food
context.getResources().getIdentifier("@drawable/food", null, context.getPackageName());
与
相同context.getResources().getIdentifier("food", "drawable", context.getPackageName());
还要确保检查您的resId是否不是0
答案 2 :(得分:0)
您做错了所有..
尝试这种方式
int resourceId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
if (resourceId != 0)
cat_img.setImageDrawable(context.getResources().getDrawable(resourceId));
else
cat_img.setImageDrawable(context.getResources().getDrawable(R.drawable.some_icon));