Я хочу сделать то, что должно быть очень простым: иметь LinearLayout, содержащий TextView и ListView.
В одной из книг CommonsWare является примером, насколько я могуВидите, фактически так же, как мой пример.Однако мой отказывает (неожиданно остановлен; получено RuntimeException), если вызывается вызов setContentView в onCreate.Комментирование приводит к тому, что список отображается нормально, но у меня нет заголовка.
Согласно Как показать другой макет в списках Я должен убедиться, что мой ListView имеет идентификатор "list",Мой делает.
Чего мне не хватает?Вот main.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">
<TextView android:text="Constant Heading" android:id="@+id/heading"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ListView android:layout_height="fill_parent" android:id="@+id/list"
android:layout_width="fill_parent" />
</LinearLayout>
listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
и вот код, который его использует:
package com.explorenm.usstates;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class USStates extends ListActivity {
static final String[] STATES = new String[] {
"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana",
"Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota",
"Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas",
"Utah", "Vermont", "Virginia", "Washington", "West
Virginia", "Wisconsin", "Wyoming"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Connect the array to the list view
setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, STATES));
//Allow the user to jump to an entry by typing
ListView lv = getListView();
lv.setTextFilterEnabled(true);
// Provide code to execute if the list item is clicked.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the text in the list item
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Вот logcatтрассировка стека:
E/AndroidRuntime( 375): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.explorenm.usstates/com.explorenm.usstates.USStates}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
E/AndroidRuntime( 375): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
E/AndroidRuntime( 375): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
E/AndroidRuntime( 375): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 375): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
E/AndroidRuntime( 375): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 375): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 375): at android.app.ActivityThread.main(ActivityThread.java:3647)
E/AndroidRuntime( 375): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 375): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 375): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 375): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
E/AndroidRuntime( 375): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
E/AndroidRuntime( 375): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
E/AndroidRuntime( 375): at android.app.Activity.setContentView(Activity.java:1657)
E/AndroidRuntime( 375): at com.explorenm.usstates.USStates.onCreate(USStates.java:31)
E/AndroidRuntime( 375): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 375): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
E/AndroidRuntime( 375): ... 11 more
Замечу, что там написано: «Ваш контент должен иметь ListView с атрибутом id« android.R.id.list »».Насколько я вижу, я делаю (android: id = "@ + id / list" в main.xml);см. полный файл выше.
Спасибо.