资源 $NotFoundException: String resource ID # 0x0

我正在开发一个 Android 应用程序,它可以从 MySQL 数据库中读取数据,我遇到了这个错误。我有这样的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >




<TextView android:id="@+id/wardNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="Ward Number"
android:textSize="22dp"/>




<TextView android:id="@+id/dateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/wardNumber"
android:layout_alignParentRight="true"
android:layout_marginRight="3dp"
android:text="Date-Time" />


<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dateTime"
android:layout_alignBottom="@+id/dateTime"
android:layout_alignLeft="@+id/wardNumber"
android:layout_marginLeft="3dp"
android:text="Name" />


</RelativeLayout>

这是我的 Java 文件:

public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_custom_list, items);
this.items = items;
}


@Override
public int getCount() {
return items.size();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;


if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_custom_list, null);
}


Application app = items.get(position);


if(app != null) {
//  ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
TextView wardNumber = (TextView)v.findViewById(R.id.wardNumber);
TextView name = (TextView)v.findViewById(R.id.name);
//  LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
TextView dateTime = (TextView)v.findViewById(R.id.dateTime);


//      if(icon != null) {
//          Resources res = getContext().getResources();
//         String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
//         icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
//     }


if(wardNumber != null) wardNumber.setText(app.getTitle());


if(name != null) name.setText(app.getTitle());


if(dateTime != null) {
dateTime.setText(app.getTotalDl());
//  NumberFormat nf = NumberFormat.getNumberInstance();
//  dateTime.setText(nf.format(app.getTotalDl())+" dl");
}




}


return v;
}
}

当调用此 Activity 时,我收到这个错误消息:

11-24 21:12:03.633:  E/AndroidRuntime(1251):  FATAL EXCEPTION: main
11-24 21:12:03.633:  E/AndroidRuntime(1251): android.content.res.Resources$NotFoundException: String resource ID #0x0
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.content.res.Resources.getText(Resources.java:201)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.TextView.setText(TextView.java:2863)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.sj.jsondemo.Appl icationAdapter.getView(ApplicationAdapter.java:53)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.ListView.onMeasure(ListView.java:1127)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.View.measure(View.java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.View.measure(View.java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.View.measure(View.java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
11-24 21:12:03.633: E/AndroidRuntime(1251):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.View.measure(View.java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at  android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.View.measure(View.java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.os.Looper.loop(Looper.java:130)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at android.app.ActivityThread.main(ActivityThread.java:3683)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at java.lang.reflect.Method.invokeNative(Native Method)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at java.lang.reflect.Method.invoke(Method.java:507)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at dalvik.system.NativeStart.main(Native Method)
11-24 21:12:08.922: I/Process(1251): Sending signal. PID: 1251 SIG: 9

我不知道如何修复这个错误。

133836 次浏览

改变

dateTime.setText(app.getTotalDl());

dateTime.setText(String.valueOf(app.getTotalDl()));

setText有不同的版本-一个采用 String,一个采用 int 资源 id。如果传递给它一个整数,它将尝试查找相应的字符串资源 id-它无法找到,这是您的错误。

我想 app.getTotalDl()返回一个整型数。您需要明确地告诉 setText将其设置为这个 int 的 String 值。

SetText (int resid) vs < a href = “ http://developer.android.com/reference/android/widget/TextView.html # setText% 28java.lang.CharSequence% 29”> setText (CharSequence text)

替换

dateTime.setText(app.getTotalDl());

dateTime.setText(""+app.getTotalDl());

当您尝试在 Edittexttextview中设置文本时 应该只传递字符串格式。

dateTime.setText(app.getTotalDl());

dateTime.setText(String.valueOf(app.getTotalDl()));

Settext 的评估值是整数,所以它去看一个资源附加到它,但没有找到,你想设置文本,所以它应该是字符串,所以转换整数字符串附加 .toStringeString.valueOf(int)将解决你的问题!

你可以的,这是个简单的方法。

txtTopicNo.setText(10+"");

如果我们得到的值为 int,并将其设置为 String,就会发生错误,

Textview = tv_property_count;
int property_id;
tv_property_count.setText(String.valueOf(property_id));

如果你得到了 int 中的值,你必须使用 string 来表示它抛出了错误

之前

  holder.villageName.setText(villageModelList.get(position).getVillageName());
holder.villageCount.setText(villageModelList.get(position).getPeopleCount());
holder.peopleCount.setText(villageModelList.get(position).getPeopleCount());

之后

  holder.villageName.setText(villageModelList.get(position).getVillageName());
holder.villageCount.setText(String.valueOf(villageModelList.get(position).getPeopleCount()));
holder.peopleCount.setText(String.valueOf(villageModelList.get(position).getPeopleCount()));

您可以通过添加 String.valueOf来解决这个错误

如果你在利用科特林

dateTime.setText(
app.getTotalDl().toString()
)

如果问题仍然存在,并且您正在使用 data binding以防万一,请重新检查您的 android:text属性。