2

我正在开发一个应用程序,该应用程序通过 Google 进行身份验证,以从他们的 Google 帐户中获取用户的个人资料图片和姓名。目前,我只是尝试打印从 Google 收到的令牌作为调试措施,以验证它是否可以正常工作。但是,我一直有一个空令牌(它似乎根本没有设置),给出以下错误:

java.lang.NullPointerException
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
com.sp.norsesquare.froyo.NorseSquare$LoginAsyncTask.doInBackground(NS.java:465)
com.sp.norsesquare.froyo.NorseSquare$LoginAsyncTask.doInBackground(NS.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:287)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
java.util.concurrent.FutureTask.run(FutureTask.java:137)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
java.lang.Thread.run(Thread.java:856)

我正在使用自定义 AsyncTask 类来获取数据并将其返回给主活动,这被定义为主活动中的公共类。

public class LoginAsyncTask extends AsyncTask<String, Void, String>
{
NS ns;
String email;
String mScope;
String authToken;
Context context;
Bundle bundle;

public LoginAsyncTask(String e)
{
    email = e;
    bundle = new Bundle();
}

protected void onPreExecute()
{
    Log.i("BEGIN","Getting authtoken");
}

protected String doInBackground(String... args)
{
    try 
    {
        authToken = GoogleAuthUtil.getToken(context, email, "oauth2:"+"https://www.googleapis.com/auth/userinfo.profile", bundle);
        Log.i("MESSAGEGEGEGE","YOUR TOKEN = "+authToken);

    }
    catch (UserRecoverableAuthException recoverableException) {
        Toast.makeText(context, "UserRecoverableException Occurred", Toast.LENGTH_LONG).show();
        Log.e("GOOGLEAUTH","UserRecoverableException Triggered");
         Intent recoveryIntent = recoverableException.getIntent();


     } catch (GoogleAuthException authEx) {

         Log.e("MESSAAGEGEG", "Unrecoverable authentication exception: " + authEx.getMessage(), authEx);
     } catch (IOException ioEx) {
         Log.i("MESSAGEGEGE", "transient error encountered: " + ioEx.getMessage());

     }
       catch (Exception e) {
           e.printStackTrace();
       }
    return authToken;
}

protected void onProgressUpdate(Integer... progress)
{
    Log.i("PROGRESS","Getting somewhere");
}

protected void onPostExecute(String result) 
{

    Log.i("GOOGLEAUTH", "Returning Received Google Token");
    googleAuthToken = result;
        //This is a variable defined in the main activity
    }


}

我不知道这是从哪里来的,在任何地方都找不到任何东西。非常感谢您的帮助,谢谢!

4

1 回答 1

0

正如 Arhimed 所建议的,您传递给的上下文getToken()可能为空。当我将一些东西拼凑在一起以进行快速而肮脏的测试时,这发生在我身上。传递一个有效的上下文解决了这个错误。

于 2013-08-22T16:35:16.073 回答