IllegalArgumentException: addAccount not supported

自作多情 提交于 2019-12-05 04:59:09

I found my mistake. The addAccount method needs some annotations. Finally it looks like this:

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

For Kotlin make sure you have nullable type parameters.

This is wrong:

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

Look at the parameters, they are nonnull in the above code. Which throws

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)

Following this the correct method definition:

 @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

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!