空指针异常 - Android中Google帐户上的GoogleAuthUtil身份验证

时间:2013-04-10 05:17:27

标签: android android-asynctask nullpointerexception

我正在制作一款应用程序,除了其他功能外,还会通过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
    }


}

我有点迷失它的来源,在任何地方找不到任何东西。我非常感谢你的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

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