Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getAuthToken not working is ICS

Tags:

android

My app use account manager to share auth token and it works fine in early 4.0. But when i try the app in my Nexus S (4.0.3) , NPE occurs every time i call the getAuthToken method. Stack trace as follows:

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

But, when i get google's AuthToken, it works fine. My question is : Are there something wrong in my code and how can i fix it? Or it is a bug in android and it will be fixed later?

like image 469
he.cao Avatar asked Nov 14 '22 08:11

he.cao


1 Answers

This issue was killing me for a while, but this worked for me on 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);

Where 'callback' looks some thing like this:

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

    }

  }
}

This method doesn't return the GrantCredentialsPermissionActivity, it returns a different one, something like 'LoginActivity'.

Hope this helps

like image 113
Theblacknight Avatar answered Nov 16 '22 04:11

Theblacknight