AccountManager IllegalArgumentException:键为空

发布于 2024-12-19 06:37:05 字数 1448 浏览 7 评论 0原文

好吧,我在不应该的地方收到了 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;
}

键是常量字符串值,但应用程序仍然抛出:

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 “走进”这个方法时,我在调试视角:

在此处输入图像描述

值不为 null >o<

Ok, I'm getting an IllegalArgumentException at a point where it shouldn't.

I have a custom extension of Account that is saved using the AccountManager:

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

The keys are constant String values but app still throws:

java.lang.IllegalArgumentException: key is null

I have to say that I'm only attaching the user data in this fashion because using:

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

didn't seem to attach the values. Do the keys require a special name pattern?

Anybody had this problem before?


Update:

It gets thrown inside the manager.setUserData() which looks like this (Android code):

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

When I "walk" into this method with eclipse I get this in the debug perspective:

enter image description here

The values aren't null >o<

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-12-26 06:37:05

好的,经过进一步研究 AccountManager I没有找到让它像这样工作的方法我正在尝试,但我找到了解决方案。

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

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

:仍然不确定这是否是存储帐户数据的方式,但这是迄今为止我唯一成功的方法。

Ok, after further research into 's AccountManager I did not find a way to make it work like I was trying but I found a solution.

Instead of saving the details as an user data bundle I save them as authToken values using the key as the authTokenType like this:

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

And then retrieving the values like this:

value1 = manager.peekAuthToken(account, KEY_1);

I'm still not sure if this is the way to store data for an Account but it's the only one I've managed to make work so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文