Android 帐户首选项在ListPreference上崩溃

Android 帐户首选项在ListPreference上崩溃,android,Android,我已经使用AccountAuthenticator创建了一个帐户类型,如SampleSyncAdapter教程中所述。我现在正在尝试让帐户首选项正常工作 我已将行android:accountPreferences=“@xml/account\u preferences”添加到我的帐户验证器中,account\u preferences.xml看起来是这样的: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen x

我已经使用AccountAuthenticator创建了一个帐户类型,如
SampleSyncAdapter
教程中所述。我现在正在尝试让帐户首选项正常工作

我已将行
android:accountPreferences=“@xml/account\u preferences”
添加到我的
帐户验证器中,account\u 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>

我在EditTextPreference和我创建的DialogPreference的自定义子类中遇到了相同的错误。

对于grins,请尝试取消选中您的
首选项类别。不管怎么说,它对你没有多大好处,因为它没有包装任何偏好。我怀疑这是您的问题,但这是我在XML中看到的唯一奇怪的事情


否则,这个异常对我来说是“Android bug”,所以如果你能创建一个复制错误的项目,把它和这个信息发布到。

也许是因为你使用的是同一个数组?将您的
条目
作为您的
条目值

尝试制作另一个数组并尝试

android:entries="@array/years"
android:entryValues="@array/years_values"

是的,这似乎是一个错误。谷歌有一些针对它的漏洞


我可以用编辑和列表首选项确认相同的问题。到目前为止,我只能让复选框在不崩溃的情况下工作。状态是持久的,但我不知道状态存储在哪里。

我遇到了同样的问题,遇到了上面提到的相同异常。在查看Android源代码后,似乎每个想要创建对话框或新窗口的首选项都会出现这种情况。这似乎是作为应用程序窗口创建的错误

在示例的文档中,使用单击意图

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/title_fmt" />
<PreferenceScreen
     android:key="key1"
     android:title="@string/key1_action"
     android:summary="@string/key1_summary">
     <intent
         android:action="key1.ACTION"
         android:targetPackage="key1.package"
         android:targetClass="key1.class" />
 </PreferenceScreen>
最大的问题是,如何赋予这一意图旗帜?我还没有看到通过XML设置意图标志的方法。我已经创建了自己的首选项,并在onClick()期间启动了intent。但是,帐户首选项似乎是在account&sync设置上下文中启动的,类加载器无法找到我的类

我在这里看到两种解决方案:

  • 将标志设置为意图
  • 子类Preference和handle onClick()启动活动。但是如何发布我自己的类呢

  • 我终于成功地启动了自定义首选项活动。问题在于,您必须保持以下XML布局(如上所述):

    
    
    以及相应的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>
    
    您可以找到以下代码行:

        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);
            }
        }
    }
    
    private void updatePreferenceContent(首选项屏幕首选项){
    for(int i=0;i
    这些将标志添加到XML中指定的意图中。但这只适用于偏好屏幕中的所有一年级儿童。我的错是我把我的意图封装在了优先类别中,所以这个标志从来没有被OR-ed


    希望我能帮忙。

    PS。这是整个堆栈跟踪。我把它放在原帖中并不是为了保持可读性。移除分类标签并没有改变任何事情。在提交bug之前,我会仔细研究一下。好吧,我认为这是一个bug,因为当我创建一个
    PreferenceActivity
    并使用
    addPreferencesFromResource
    指向同一个xml文件时,它工作得很好。这可能是一个bug,也可能是Android团队的一个糟糕的设计选择。但是说它本身并不能解决问题。对于
    AndroidRuntimeException
    ,请看一看:在android清单中,您可以为同一个活动标签放置多个操作和类别,因为此帐户首选项活动将从android启动;s设置帐户屏幕,可能还有工具栏上的应用程序设置,可能还有其他地方,您可以定义多个操作。关键是这个帐户首选项实现被多次使用。
    <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>
    
    <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>
    
        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);
            }
        }
    }