Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView [duplicate]

Tags:

java

android

I tried this tutorial http://windrealm.org/tutorials/android/android-listview.php "A Simple Android ListView Example"

But in my test in Eclipse I've crash in android application.

In LogCat I've this:

04-11 20:17:24.170: E/AndroidRuntime(7062): 
java.lang.IllegalStateException: ArrayAdapter requires the resource ID
to be a TextView

Why the crash ?

class java

private ListView mainListView;
private ArrayAdapter<String> listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mains);

        mainListView = (ListView) findViewById(R.id.mainListView);

        String[] xRemote = Remote.split(";");

        ArrayList<String> planetList = new ArrayList<String>();

        planetList.addAll(Arrays.asList(xRemote));

        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
                planetList);

        listAdapter.addAll(xRemote);

        mainListView.setAdapter(listAdapter);

mains.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

        <ListView android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:id="@+id/mainListView">
        </ListView      
    </LinearLayout>

simplerow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/rowTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

</LinearLayout>
like image 947
Hamamelis Avatar asked Apr 11 '14 18:04

Hamamelis


2 Answers

Use

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
    R.id.rowTextView, planetList);

The documentation says:

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

like image 94
Emanuel Moecklin Avatar answered Sep 28 '22 03:09

Emanuel Moecklin


In simplerow.xml remove the LinearLayout and directly use TextView as the root

like image 27
Benoit Avatar answered Sep 28 '22 04:09

Benoit