Reputation: 387
I'm trying to run an example I found it on a book about the ListView
. As the book stated, I created the string-array
in string.xml
file and the layout of the ListView
is in list_item.xml
inside res->layout
folder.
The problem is that, the app crashes when I run the activity. Can any one tell me where the mistake is?
Code1:
public class ListViewTestActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] c = getResources().getStringArray(R.array.cities);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, c));
}}
string.xml
<resources>
<string name="hello">Hello World, ListViewTestActivity!</string>
<string name="app_name">ListViewTest</string>
<string-array
name="cities">
<item>Bath</item>
<item>Birmingham</item>
<item>Bradford</item>
<item>Brighton</item>
<item>Bristol</item>
<item>Cambridge</item>
<item>Canterbury</item>
</string-array>
list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28dip">
</TextView>
</LinearLayout>
LogCat:
05-05 22:45:16.018: E/ArrayAdapter(648): You must supply a resource ID for a TextView
05-05 22:45:16.048: E/AndroidRuntime(648): FATAL EXCEPTION: main
05-05 22:45:16.048: E/AndroidRuntime(648): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ListView.makeAndAddView(ListView.java:1745)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ListView.fillDown(ListView.java:670)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ListView.fillFromTop(ListView.java:727)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ListView.layoutChildren(ListView.java:1598)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.View.layout(View.java:7175)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.os.Looper.loop(Looper.java:130)
05-05 22:45:16.048: E/AndroidRuntime(648): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-05 22:45:16.048: E/AndroidRuntime(648): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 22:45:16.048: E/AndroidRuntime(648): at java.lang.reflect.Method.invoke(Method.java:507)
05-05 22:45:16.048: E/AndroidRuntime(648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-05 22:45:16.048: E/AndroidRuntime(648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-05 22:45:16.048: E/AndroidRuntime(648): at dalvik.system.NativeStart.main(Native Method)
05-05 22:45:16.048: E/AndroidRuntime(648): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-05 22:45:16.048: E/AndroidRuntime(648): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
Upvotes: 1
Views: 2062
Reputation: 168
You are probably closing your linear layout in list_item.xml
.
If that does not work, post log
Upvotes: 0
Reputation: 87064
If you use that particular layout for the row item then assign an id
to the TextView
and pass that in the constructor of the adapter to let the adapter know where to place the data:
<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/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28dip">
</TextView>
and in your activity:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, c));
Upvotes: 2
Reputation: 611
You probably need a call to setContentView(...) somewhere in onCreate.
Upvotes: 0
Reputation: 583
Your xml file must be like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/monid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28dip" />
Upvotes: 0