2010-05-14 64 views
4

我使用AccountAuthenticator的东西创建了一个帐户类型,如在SampleSyncAdapter教程中所做的那样。我现在试图获取帐户首选项的工作。帐户偏好在ListPreference上崩溃

我添加了这行android:accountPreferences="@xml/account_preferences"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> 

复选框优先工作完全像它应该,但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 

我得到了与EditTextPreference和我创建的DialogPreference的自定义子类相同的错误。

+0

PS。这是整个堆栈跟踪。我没有把它放在原来的文章中,试图保持可读性。 http://gist.github.com/raw/401785/73141d95ebddab0fc67b6219c38701c8d3ac7051/gistfile1.txt – Sionide21 2010-05-14 22:36:44

回答

-1

对于咧嘴笑,试试看你的PreferenceCategory。无论如何,它并没有让你变得很好,因为它没有包含任何偏好。我怀疑这是你的问题,但这是我看到的唯一奇怪的事情。

否则,该异常将“Android bug”扫描给我,因此如果您可以创建重现错误的项目,请将其发布并将其发送到http://b.android.com

+0

删除该类别标记没有改变任何东西。在提交bug之前,我会再多一点。 – Sionide21 2010-05-15 18:39:00

+0

好吧,我认为这是一个错误,因为当我创建一个'PreferenceActivity'并使用'addPreferencesFromResource'指向同一个xml文件时,它工作得很好。 – Sionide21 2010-05-16 04:04:24

+0

这可能是Android团队的一个错误或糟糕的设计选择。但说它不能自己解决问题。 – petersaints 2013-06-17 03:04:52

-1

也许是因为你使用了相同的数组?为您的entries作为您的entryValues

尝试使另一个数组,并试图

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

是这似乎是一个错误。 Google有一些错误。

我可以用编辑和列表首选项确认相同的问题。到目前为止,我只能使CheckBox工作而不会崩溃。这个状态是持久的,但我不知道状态的存储位置。

1

我得到了同样的问题,并遇到了上述相同的异常。查看Android源代码后,似乎发生这种情况,这种情况发生在想要创建对话框或新窗口的每个首选项上。这似乎被创建为APPLICATION_WINDOW什么是错的。

在文档AbstractAccountAuthenticator该示例使用一个意图点击。

<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> 

我认为其目的是从账户喜好推出新的优惠活动,在地方不使用它们。不好的是这让我们弹出一个新的例外:

10-01 09:33:36.935: ERROR/AndroidRuntime(52): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 

最大的问题是如何给意图的标志?我没有看到通过XML设置意图标志的方法。我已经创建了自己的偏好,并在onClick()期间启动了这个意图。但似乎帐户首选项是在&同步设置上下文中启动的,而类加载器无法找到我的类。

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

  1. 设置标志的意图。
  2. 子类首选项并处理onClick()以启动您的活动。但是如何发布我自己的课程?
+0

对于'AndroidRuntimeException',看看这个:http://stackoverflow.com/questions/14328253/launch-intent-from-account-authenticator-accountpreferences-screen – 2013-02-21 14:00:00

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> 

而且根据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> 

如果您在Android code看你能找到的代码下列行:

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); 
     } 
    } 
} 

和这些标志添加到XML中指定的意图。但这只适用于PreferenceScreen的所有1年级的孩子。我的错是我将Intent封装在PreferenceCategory中,所以这个标志从来没有OR-ed。

希望我能帮上忙。

+0

在Android清单中,您可以将多个操作和类别对于同一Activity 标记,并且此帐户首选项活动将从android; s设置帐户屏幕启动,可能还会在工具栏上以及可能在其他位置定义多个操作的应用程序设置中启动。关键是这个帐户首选项实施不止一次。 – Pomagranite 2016-07-24 22:58:35