为什么我必须有列表视图?

时间:2011-02-23 02:44:08

标签: android listview

我正在尝试实施受密码保护的首选项屏幕。我按下菜单按钮,然后按设置按钮,而不是显示密码屏幕,FC和我收到此错误:

Uncaught handler: thread main exiting due to uncaught exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.john.websafe/com.john.websafe.Preferences}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
     <snip>
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
     at android.app.ListActivity.onContentChanged(ListActivity.java:236)
     at android.preference.PreferenceActivity.onContentChanged(PreferenceActivity.java:160)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
     at android.app.Activity.setContentView(Activity.java:1626)
     at com.john.websafe.Preferences.onCreate(Preferences.java:19)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
     ... 11 more

Preferences.java第19行:

setContentView(R.layout.password);

Password.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:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/password" />

<EditText android:id="@+id/password" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scrollbars="vertical" />

<Button android:id="@+id/confirm" 
  android:text="@string/confirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

导致此错误的原因是什么?为什么我需要一个id为android.R.list的列表视图?我甚至没有 ListActivity ... PreferenceActivity是否包含ListActivity

2 个答案:

答案 0 :(得分:8)

是的,PreferenceActivityListActivitysource)的子类,因此您可以删除setContentView()方法调用或添加标识为{{1}的子视图} android.R.list

答案 1 :(得分:1)

对于任何有兴趣的人,我通过将密码和首选项活动分成密码活动和PreferenceActivity来解决问题,因此意味着扩展PreferenceActivity的活动没有调用{{1 (基本上,我做了what ZelluX said)。