"ArrayAdapter要求资源ID为TextView"问题 [英] “ArrayAdapter requires the resource ID to be a TextView” issue

查看:68
本文介绍了"ArrayAdapter要求资源ID为TextView"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在为ListView使用自定义样式时遇到问题.

I seem to have problems using custom styles for ListView.

所有在线搜索(stackOverflow和其他站点)都这么说:-创建一个my_style.xml布局文件.-在适配器中使用它.

All online searches (stackOverflow and other sites) pty much say the same: - create an my_style.xml layout file. - use it in the adapter.

但出于某种原因,这似乎对我不起作用.MainAct.java:

But for some reason this doesn't seem to be working for me. MainAct.java:

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.list_content, items);

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#0000DD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

另存为/res/layout/list_content.xml.

saved as /res/layout/list_content.xml.

如果我使用android.R.layout.simple_list_item_1,则一切正常,使用自己的xml文件,我会收到"java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView".

If I use the android.R.layout.simple_list_item_1 everything works fine, with the own xml file I get "java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView".

有什么想法吗?

PS:我已经看过许多其他非常相似的问题/问题,包括:

PS: I already looked at lots of other very similar questions/issues, including:

"ArrayAdapter要求资源ID为TextView".xml问题

如何在ListView android应用程序的中央设置文本?

Android ListView文本颜色

推荐答案

错误 java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView 明确指出.您需要传递需要显示数据的textview.另外,当您使用自己的布局时, R.java 应该属于您的项目,而不是 android.R .

As error java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView clearly says. You need to pass the textview on which you need to show the data. Also, when you are using your own layout then R.java should belong to your project not android.R.

更改

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.list_content, items);

arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_content, R.id.textview, items);

这篇关于"ArrayAdapter要求资源ID为TextView"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆