我正在开发一个从MySQL数据库读取数据的Android应用程序,我遇到了这个错误。我有这样的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
我不知道如何修复这个错误。
替换
dateTime.setText(app.getTotalDl());
与
dateTime.setText(""+app.getTotalDl());
改变
dateTime.setText(app.getTotalDl());
至
dateTime.setText(String.valueOf(app.getTotalDl()));
settext
有不同的版本--一个采用字符串,另一个采用int资源ID。如果您传递给它一个整数,它将尝试寻找相应的字符串资源id--它找不到,这就是您的错误。
我猜app.gettotaldl()
返回一个int。您需要特别地告诉settext
将其设置为这个int的字符串值。
setText(int resid)vs setText(CharSequence文本)
问题内容: 我正在开发一个Android应用程序,该程序从MySQL数据库读取数据,但遇到了此错误。我有这个XML布局: 这是我的Java文件: 调用此Activity时,我收到以下错误消息: 我不知道如何解决此错误。 问题答案: 更改 至 有不同的版本-一种采用字符串,一种采用int资源ID。如果将其传递给整数,它将尝试查找相应的字符串资源ID-找不到它,这是你的错误。 我想返回一个整数。你需要
我仍然不明白错误在哪里,如何恢复它!
回收器自定义适配器 01-09 10:43:45.747 660 2-6602/Ctize.connectplus.com.communitize E/AndroidRuntime:致命异常:主进程:Ctize.connectplus.com.communitize,PID:6602 Android.content.res.resources$NotFoundException:资源ID#0x7F
问题内容: 运行片段尝试显示对象的ListView时会发生此异常。我已经研究了该异常,但针对其他情况的解决方案似乎与“ setText”函数有关,或者其他任何尝试将除字符串以外的任何内容显示为字符串的问题。我真的不确定还有什么要说的。提前致谢! Java类 } XML布局 编辑(日志) 问题答案: 发现您的错误。它在您的IngAdapter中的以下行中 在此,第二个参数应该是资源名称,并且绝不能使
android.content.res.resources$NotFoundException:资源ID#0x0 错误信息 E/AndroidRuntime:致命异常:main 进程:com.example.sozluk,pid:2926 Android.content.res.resources$NotFoundException:资源ID#0x0