帐户首选项在ListPreference上崩溃

Sio*_*e21 4 android

我已经使用AccountAuthenticator创建了一个帐户类型,如SampleSyncAdapter教程中所做的那样.我现在正试图让帐户首选项工作.

我已将行添加android:accountPreferences="@xml/account_preferences"到my account-authenticator和account_preferences.xml中,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/alum_settings_title"/>

<CheckBoxPreference
    android:key="sync_alum"
    android:title="@string/sync_alum"
    android:summaryOn="@string/sync_alum_check"
    android:summaryOff="@string/sync_alum_nocheck"/>

<ListPreference
    android:key="sync_alum_since"
    android:title="@string/alum_years"
    android:entries="@array/years"
    android:entryValues="@array/years"
    android:dependency="sync_alum"/>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

复选框首选项的工作方式与它应该完全相同,但ListPreference会使用以下消息使整个系统崩溃:

05-14 22:32:16.794: ERROR/AndroidRuntime(63): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Run Code Online (Sandbox Code Playgroud)

我使用EditTextPreference和我创建的DialogPreference的自定义子类得到了相同的错误.

小智 6

我终于成功启动了自定义首选项活动.陷阱是你必须保持以下XML布局(如上所述):

<PreferenceScreen
   xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
   android:title="Account settings" />
<PreferenceScreen
   android:key="key"
   android:title="General settings"
   android:summary="Some summary">

   <!-- package relative class name-->
   <intent
      android:action="my.account.preference.MAIN"          
      android:targetClass="prefs.AccountPreferencesActivity.class">
   </intent>          
</PreferenceScreen>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

并根据AndroidManifest.xml条目:

<activity
   android:label="Account preferences"
   android:name=".prefs.AccountPreferencesActivity">
   <intent-filter>
      <action
         android:name="my.account.preference.MAIN" />
      <category
         android:name="android.intent.category.DEFAULT" />
    </intent-filter>
 </activity>
Run Code Online (Sandbox Code Playgroud)

如果你查看Android代码,你可以找到以下代码行:

    private void updatePreferenceIntents(PreferenceScreen prefs) {
    for (int i = 0; i < prefs.getPreferenceCount(); i++) {
        Intent intent = prefs.getPreference(i).getIntent();
        if (intent != null) {
            intent.putExtra(ACCOUNT_KEY, mAccount);
            // This is somewhat of a hack. Since the preference screen we're accessing comes
            // from another package, we need to modify the intent to launch it with
            // FLAG_ACTIVITY_NEW_TASK.
            // TODO: Do something smarter if we ever have PreferenceScreens of our own.
            intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这些将标志添加到XML中指定的Intent.但这仅适用于PreferenceScreen的所有一年级儿童.我的错是我在PreferenceCategory中封装了我的Intent,因此该标志从未被OR-ed.

希望我能提供帮助.