AccountManager IllegalArgumentException:key为null

时间:2011-12-02 17:39:43

标签: java android accountmanager accounts

好的,我得到一个IllegalArgumentException,它不应该。

我有一个使用AccountManager保存的Account自定义扩展名:

// Method inside a custom extension of Account
public boolean save(AccountManager manager) {
    removeAll(manager);
    boolean result = manager.addAccountExplicitly(this, null, toBundle());
    manager.setUserData(this, KEY_1, value1);
    manager.setUserData(this, KEY_2, value2);
    manager.setUserData(this, KEY_3, value3);
    return result;
}

键是常量字符串值,但app仍然会抛出:

java.lang.IllegalArgumentException: key is null

我必须说我只是以这种方式附加用户数据,因为使用:

 manager.addAccountExplicitly(this, null, toBundle());

似乎没有附加值。密钥是否需要特殊的名称模式?

以前有人遇到过这个问题吗?


更新

它被抛入manager.setUserData()里面,看起来像这样(Android代码):

public void setUserData(final Account account, final String key, final String value) {
    if (account == null) throw new IllegalArgumentException("account is null");
    if (key == null) throw new IllegalArgumentException("key is null");
    try {
        mService.setUserData(account, key, value);
    } catch (RemoteException e) {
        // won't ever happen
        throw new RuntimeException(e);
    }
}

当我用eclipse“走进”这个方法时,我在调试透视图中得到了这个:

enter image description here

值不为空> o<

1 个答案:

答案 0 :(得分:1)

好的,经过对 AccountManager的进一步研究后,我没有找到办法让它像我一样努力,但我找到了解决办法。

我没有将详细信息保存为用户数据包,而是使用密钥authToken将其保存为authTokenType值,如下所示:

public boolean save(AccountManager manager) {
    removeAll(manager);
    boolean result = manager.addAccountExplicitly(this, null, toBundle());
    manager.setAuthToken(this, KEY_1, value1);
    manager.setAuthToken(this, KEY_2, value2);
    manager.setAuthToken(this, KEY_3, value3);
    return result;
}

然后检索这样的值:

value1 = manager.peekAuthToken(account, KEY_1);

我仍然不确定这是否是存储Account数据的方式,但它是我迄今为止唯一能够完成工作的方法。