I'm having problems trying to run an Android activity which implements a ListView, fills it with 20 elements and displays it: the activity crashes upon launch.
Here are the relevant LogCat errors:
You must supply a resource ID for a TextView
FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
at android.widget.ListView.onMeasure(ListView.java:15848)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(ListView.java:1159)
at android.view.View.measure(View.java:15848)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.app.ActivityThread.main(ActivityThread.java:5013)
Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.TextView.
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:382)
ThirdActivity.java
package com.example.logger;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ThirdActivity extends ListActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final ListView listview = (ListView) findViewById(android.R.id.list);
String[] values = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
"Twenty"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_third, android.R.id.list, values);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;
}
public void attachButton()
{
findViewById(R.id.add).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
End .java
And the layout:
Activity_third.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ThirdActivity" >
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/add"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_below="@+id/add"
android:layout_height="wrap_content"
></ListView>
</RelativeLayout>
End .xml
Could you please tell me what I need to change to get it to work based on the LogCat error logs?
Thank you very much for your help.
Yours sincerely,
Mauro.
ArrayAdapter
requires the resource ID to be aTextView
This just means, that you have to supply a layout with a TextView
as an argument. The Adapter will bind this TextView
to your data. But you are currently passing in your layout with your ListView
in it.
So change this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_third, android.R.id.list, values);
to this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);