非法参数异常:不支持addAccount

8

我按照这个说明添加了我的同步适配器。

但是有一个小问题 :-(

当我打开设置 -> 帐户 -> 添加帐户并选择我的帐户时,我收到了这个错误消息:

java.lang.IllegalArgumentException: addAccount not supported
    at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2147)
    at android.accounts.AccountManager.-wrap0(AccountManager.java)
    at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1993)
    at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
    at android.os.Binder.execTransact(Binder.java:453)

看起来这个崩溃是由将Authenticator Java类转换为Kotlin类引起的。

Java类如下所示:

public class Authenticator extends AbstractAccountAuthenticator {

    private final Context mContext;

    // Simple constructor
    public Authenticator(Context context) {

        super(context);
        mContext = context;
    }

    // Editing properties is not supported
    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response,
                                 String accountType) {

        throw new UnsupportedOperationException();
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
                             String authTokenType, String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {

        SyncUtilsKt.createSyncAccount(mContext);

        final Bundle bundle = new Bundle();
        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, ConstantsKt.SYNC_ACCOUNT);
        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE);

        return bundle;
    }

    // Ignore attempts to confirm credentials
    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response,
                                     Account account, Bundle options)
            throws NetworkErrorException {

        return null;
    }

    // Getting an authentication token is not supported
    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response,
                               Account account, String authTokenType, Bundle options)
            throws NetworkErrorException {

        throw new UnsupportedOperationException();
    }

    // Getting a label for the auth token is not supported
    @Override
    public String getAuthTokenLabel(String authTokenType) {

        throw new UnsupportedOperationException();
    }

    // Updating user credentials is not supported
    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response,
                                    Account account, String authTokenType, Bundle options) throws NetworkErrorException {

        throw new UnsupportedOperationException();
    }

    // Checking features for the account is not supported
    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response,
                              Account account, String[] features) throws NetworkErrorException {

        throw new UnsupportedOperationException();
    }
}

在将其转换为 Kotlin 后,它看起来像这样:
class Authenticator(private val mContext: Context) : AbstractAccountAuthenticator(mContext) {

    // Editing properties is not supported
    override fun editProperties(response: AccountAuthenticatorResponse,
                                accountType: String): Bundle {

        throw UnsupportedOperationException()
    }

    @Throws(NetworkErrorException::class)
    override fun addAccount(response: AccountAuthenticatorResponse, accountType: String,
                            authTokenType: String, requiredFeatures: Array<String>, options: Bundle): Bundle {

        createSyncAccount(mContext)

        val bundle = Bundle()
        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, SYNC_ACCOUNT)
        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.ACCOUNT_TYPE)

        return bundle
    }

    // Ignore attempts to confirm credentials
    @Throws(NetworkErrorException::class)
    override fun confirmCredentials(response: AccountAuthenticatorResponse,
                                    account: Account, options: Bundle): Bundle? {

        return null
    }

    // Getting an authentication token is not supported
    @Throws(NetworkErrorException::class)
    override fun getAuthToken(response: AccountAuthenticatorResponse,
                              account: Account, authTokenType: String, options: Bundle): Bundle {

        throw UnsupportedOperationException()
    }

    // Getting a label for the auth token is not supported
    override fun getAuthTokenLabel(authTokenType: String): String {

        throw UnsupportedOperationException()
    }

    // Updating user credentials is not supported
    @Throws(NetworkErrorException::class)
    override fun updateCredentials(response: AccountAuthenticatorResponse,
                                   account: Account, authTokenType: String, options: Bundle): Bundle {

        throw UnsupportedOperationException()
    }

    // Checking features for the account is not supported
    @Throws(NetworkErrorException::class)
    override fun hasFeatures(response: AccountAuthenticatorResponse,
                             account: Account, features: Array<String>): Bundle {

        throw UnsupportedOperationException()
    }
}

我认为有些东西转换出了问题,但是是什么呢?


如жһњж‚Ёжњ‰и‡Ғе·±зљ„AbstractAccountAuthenticatorпәЊеЏҮиѓҢйњЂи¦Ѓжњ‰дғғжџӨзњ‹е…¶д»Әз ЃгЂ‚ - pskink
3个回答

9

我发现了自己的错误。 addAccount 方法需要一些注解。最终它看起来像这样:

@Throws(NetworkErrorException::class)
override fun addAccount(response: AccountAuthenticatorResponse,
                        accountType: String,
                        authTokenType: String?,
                        requiredFeatures: Array<String>?,
                        options: Bundle?): Bundle? {

    val accountManager = mContext.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager
    val accounts = accountManager.getAccountsByType("example.com")
    if (accounts.size == 0) {
        val newAccount = Account("Sync Account", "example.com")
        accountManager.addAccountExplicitly(newAccount, null, null)
    }

    val bundle = Bundle()
    bundle.putString(AccountManager.KEY_ACCOUNT_NAME, "Sync Account")
    bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, "example.com")

    return bundle
}

3

如果您要使用Kotlin,请确保您拥有可为空的类型参数。

以下写法是错误的:

@Throws(NetworkErrorException::class)
override fun addAccount(
response: AccountAuthenticatorResponse, accountType: String, authTokenType: 
String, requiredFeatures: Array<String>, options: Bundle): Bundle {

val bundle = Bundle()
val intent = Intent(context, LoginActivity::class.java)
intent.putExtra(SessionConstants.GO_ACCOUNT_TYPE, accountType)
intent.putExtra(SessionConstants.GO_AUTH_TOKEN_TYPE, authTokenType)
intent.putExtra(SessionConstants.GO_IS_ADDING_NEW_ACCOUNT, true)

intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
bundle.putParcelable(AccountManager.KEY_INTENT, intent)
return bundle
}

看一下上面的代码中的参数,它们都是非空的。如果为空会抛出异常。

java.lang.IllegalArgumentException: addAccount not supported
at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2147)
at android.accounts.AccountManager.-wrap0(AccountManager.java)
at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1993)
at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
at android.os.Binder.execTransact(Binder.java:453)

以下是正确的方法定义:

按照这个方法进行定义:

 @Throws(NetworkErrorException::class)
 override fun addAccount(response: AccountAuthenticatorResponse?, 
 accountType: String?, authTokenType: String?, p3: Array<out String>?, p4: 
 Bundle?): Bundle {
val bundle = Bundle()
val intent = Intent(context, LoginActivity::class.java)
intent.putExtra(SessionConstants.GO_ACCOUNT_TYPE, accountType)
intent.putExtra(SessionConstants.GO_AUTH_TOKEN_TYPE, authTokenType)
intent.putExtra(SessionConstants.GO_IS_ADDING_NEW_ACCOUNT, true)

intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
bundle.putParcelable(AccountManager.KEY_INTENT, intent)
return bundle

}


1
根据文档responseaccountType参数永远不会为空。 - Bryan

0

你可以忽略网络异常,但参数需要可以为空。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接