Runtime error prompt in Android Studio: arrayadapter requires the resource ID to be a textview problem

At first, listview uses Android’s own simple_ list_ item_1 display simple text. Use the default attribute, and the code is as follows:

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

After that, when the function is updated, you need to change the font color of item and specify the typeface. Record is customized in RES/layout/folder_ text.xml File:

<?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>

Change the code in. Java file to:

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

 //Change the item display text to a custom font
 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);

Run, error prompt: arrayadapter requires the resource ID to be a textview. Finally, we found the problem of. XML file. The root label must be textview and cannot nest LinearLayout. After modification, the error will be resolved.

<?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"
    />

 

Read More: