AndroidStudio中运行时错误提示:ArrayAdapter requires the resource ID to be a TextView问题

刚开始,ListView用Android自带的simple_list_item_1显示简单文本。使用默认属性,代码如下:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, adapterData);
listView.setAdapter(adapter);

之后,功能更新时需要更改item字体颜色及规定typeface,res/layout/文件夹下自定义了record_text.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/TView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#2d373c"
        android:textSize="18sp"
        />
</LinearLayout>

将.java文件中代码改为:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(RecordActivity.this,
         R.layout.record_text, adapterData);
 listView.setAdapter(adapter);

 //item显示文本改为自定义字体
 LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = layoutInflater.inflate(R.layout.record_text, null);
 TextView w = (TextView)view.findViewById(R.id.TView);
 Typeface tfsisan03 = Typeface.createFromAsset(getAssets(), "fonts/sisan03_0.ttf");
 w.setTypeface(tfsisan03);

运行,错误提示:ArrayAdapter requires the resource ID to be a TextView。最后发现是.xml文件的问题。根标签必须是TextView,不能嵌套LinearLayout等,修改之后,错误解决。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TView"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textColor="#2d373c"
    android:layout_marginLeft="10dp"
    android:paddingLeft="10dp"
    android:gravity="center_vertical"
    android:textSize="16sp"
    />



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值