Why must I have a listview?
I'm trying to implement a password-protected preferences screen. I press the menu button then the settings button and instead of the password screen showing up, it FCs and I receive this error:
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
line 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="wr开发者_运维问答ap_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>
What is causing this error? Why do I need a listview whose id is android.R.list
? I don't even have a ListActivity
... Does a PreferenceActivity
include a ListActivity
?
Yes, PreferenceActivity
is a subclass of ListActivity
(source), so you can either remove the setContentView()
method call or add a subview with id android.R.list
in password.xml
.
For anyone interested, I solved the problem by separating the Password-and-Preferences activity into an activity for the password and a PreferenceActivity
thus meaning an activity that extends PreferenceActivity
was not calling setContentView()
(Basically, I did what ZelluX said).
精彩评论