ListActivity使用整体布局提供非滚动标题

时间:2011-06-29 12:04:15

标签: android android-layout

我想做一些非常简单的事情:让一个包含TextView和ListView的LinearLayout。

其中一个CommonsWare books是一个例子,据我所知,它实际上与我的例子相同。但是,如果调用onCreate中的setContentView调用,则mine失败(意外停止;收到RuntimeException)。评论它会使列表显示正常,但我没有标题。

根据How to show another layout in listactivity,我必须确保我的ListView的id为“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中);请参阅上面的完整文件。

感谢。

3 个答案:

答案 0 :(得分:1)

啊哈。我找到了。我有

android:id="@+id/list"

何时需要

android:id="@android:id/list"

答案 1 :(得分:0)

android:layout_height="wrap_content"TextView上设置ListView,并在android:layout_weight="1"上设置ListView

<?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="wrap_content" android:id="@+id/list"
                android:layout_width="fill_parent" android:layout_weight="1"/>
</LinearLayout>

这将导致ListView展开以填充可用区域。

答案 2 :(得分:0)

在TextView上设置android:layout_height="wrap_content"。在ListView上设置android:layout_weight="1" android:layout_height="0dp"