getAuthToken不起作用是ICS

时间:2012-03-21 03:18:59

标签: android

我的应用使用帐户管理员共享身份验证令牌,它在4.0之前正常运行。 但是当我在Nexus S(4.0.3)中尝试应用程序时,每次调用getAuthToken方法时都会发生NPE。 堆栈跟踪如下:

E/AndroidRuntime( 5282): java.lang.RuntimeException: Unable to start activity ComponentInfo{android/android.accounts.Gra
ntCredentialsPermissionActivity}: java.lang.NullPointerException
E/AndroidRuntime( 5282):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 5282):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 5282):        at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 5282):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 5282):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 5282):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 5282):        at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 5282):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5282):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 5282):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 5282):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 5282):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 5282): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 5282):        at android.accounts.GrantCredentialsPermissionActivity.onCreate(GrantCredentialsPermissi
onActivity.java:84)
E/AndroidRuntime( 5282):        at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 5282):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 5282):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 5282):        ... 11 more
W/ActivityManager(  151):   Force finishing activity android/.accounts.GrantCredentialsPermissionActivity

但是,当我得到谷歌的AuthToken时,它运行正常。 我的问题是: 我的代码中有什么问题,我该如何解决? 或者它是android中的一个错误,它将在以后修复?

1 个答案:

答案 0 :(得分:1)

这个问题让我感到很沮丧,但这对ICS 4.0.3来说很有用:

AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType("com.google");
Account selected = accounts[0];
am.getAuthToken(account, "ah", false, callback, null);

'回调'看起来像这样:

new AccountManagerCallback<Bundle>() {

@Override
public void run(AccountManagerFuture<Bundle> future) {
    Bundle bundle;

    try {

        bundle = future.getResult();
        Intent intent = (Intent)bundle.get(AccountManager.KEY_INTENT);
        if(intent != null){                                        

            startActivity(intent);
    //In the activity that runs this I check for a flag in the onResume, which tells me that we returned from this activity

        }
        else{
            //we have the token!
        }

    } catch (OperationCanceledException e) {

    } catch (AuthenticatorException e) {

    } catch (IOException e) {

    }

  }
}

此方法不返回GrantCredentialsPermissionActivity,它返回另一个,例如'LoginActivity'。

希望这有帮助