您的位置:首页 > 产品设计 > UI/UE

ArrayAdapter requires the resource ID to be a TextView问题整理

2016-09-27 16:27 661 查看
今天在编写一个ActionBar的应用的时候,出现了一个错误,错误如下:

09-27 16:20:28.331: D/AndroidRuntime(18088): Shutting down VM

09-27 16:20:28.333: E/AndroidRuntime(18088): FATAL EXCEPTION: main

09-27 16:20:28.333: E/AndroidRuntime(18088): Process: com.example.gunqustudy, PID: 18088

09-27 16:20:28.333: E/AndroidRuntime(18088): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

……

09-27 16:20:28.333: E/AndroidRuntime(18088): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

……

这个异常的意思是ArrayAdapter需要的资源ID必须是TextView的。

最初错误代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="90dp"

    android:layout_height="wrap_content"

    android:orientation="horizontal" >

    <TextView 

        android:layout_width="match_parent"

        android:layout_height="30dp"

        android:gravity="center"

        android:id="@+id/textview"

        />

</LinearLayout>

正确代码:

<?xml version="1.0" encoding="utf-8"?>

<TextView 

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="30dp"

    android:gravity="center"

    android:id="@+id/textview"

    />

因为根节点必须是TextView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: