尝试使用Twitter登录Firebase时出现NullPointerException

时间:2018-04-06 13:53:47

标签: android firebase twitter nullpointerexception

我正在创建一个需要使用Twitter帐户登录作为身份验证方法的应用。然后它将Twitter帐户链接到Firebase。

我认为签名工作正常,但是与Firebase链接存在问题,就像某些数据未正确检索导致NullPointException一样。

我的活动有两个不同名称的Twitter签名按钮。

这是我的onCreate()

 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //Twitter.initialize(this);
    TwitterConfig config = new TwitterConfig.Builder(this)
            .logger(new DefaultLogger(Log.DEBUG))
            .twitterAuthConfig(new TwitterAuthConfig(twitterKey,twitterSecret))
            .debug(true)
            .build();
    Twitter.initialize(config);
    setContentView(R.layout.activity_sign);

这是将回调设置为Twitter登录按钮的功能:

void setTwitterButtonsPress()
{
   final Callback<TwitterSession> callback= new Callback<TwitterSession>() {
       @Override
       public void success(final Result<TwitterSession> result) {
           progressBar.setVisibility(View.VISIBLE);
           progressBar.getParent().bringChildToFront(progressBar);
           progressBarVisibility=true;
           AuthCredential credential = TwitterAuthProvider.getCredential(
                   result.data.getAuthToken().token,
                   result.data.getAuthToken().secret);

          mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {///this is where i get the error
               @Override
               public void onComplete(@NonNull Task<AuthResult> task) {
                   progressBar.setVisibility(View.GONE);
                   progressBarVisibility=false;
               }
           }).addOnFailureListener(new OnFailureListener() {
               @Override
               public void onFailure(@NonNull Exception e) {
                   if(e.getMessage().equals("A network error (such as timeout, interrupted connection or unreachable host) has occurred."))
                   {
                       Toast.makeText(getBaseContext(),getString(R.string.noConnection),Toast.LENGTH_LONG).show();
                   }
                   else if(e.getMessage().contains("If you believe the country of origin was incorrectly identified"))
                   {
                       Toast.makeText(getBaseContext(),getString(R.string.notAvailableInLocation),Toast.LENGTH_LONG).show();
                   }
                   else if(e.getMessage().startsWith("An account already exists with the same email address but different sign-in credentials"))
                   {
                       Toast.makeText(getBaseContext(),getString(R.string.other_credential),Toast.LENGTH_LONG).show();
                   }
                   else
                   {
                       Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                   }
                   CookieSyncManager.createInstance(getBaseContext());
                   CookieManager cookieManager = CookieManager.getInstance();
                   cookieManager.removeSessionCookie();
                   //Twitter.getSessionManager().clearActiveSession();
                   //Twitter.logOut();
                   TwitterCore.getInstance().getSessionManager().clearActiveSession();
               }
           }).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
               @Override
               public void onSuccess(AuthResult authResult) {
                   Intent intent=new Intent(getBaseContext(),add_password_activity.class);
                   intent.putExtra("checked",true);
                   startActivity(intent);
                   finish();
               }
           });
       }

       @Override
       public void failure(TwitterException e) {
            Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
       }
   };
   TwitterSignUp.setCallback(callback);
   twitterSignIn.setCallback(callback);
}

这是onActivityResult()函数,只有最后两行很重要:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManagerFacebook.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == googleSign)
    {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {

            GoogleSignInAccount account = task.getResult(ApiException.class);
            AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.getParent().bringChildToFront(progressBar);
            progressBarVisibility=true;
            mAuth.signInWithCredential(credential)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            progressBarVisibility=false;
                        }
                    }).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    Intent intent=new Intent(getBaseContext(),add_password_activity.class);
                    intent.putExtra("checked",true);
                    startActivity(intent);
                    finish();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if(e.getMessage().equals("A network error (such as timeout, interrupted connection or unreachable host) has occurred."))
                    {
                        Toast.makeText(getBaseContext(),getString(R.string.noConnection),Toast.LENGTH_LONG).show();
                    }
                    else if(e.getMessage().contains("If you believe the country of origin was incorrectly identified"))
                    {
                        Toast.makeText(getBaseContext(),getString(R.string.notAvailableInLocation),Toast.LENGTH_LONG).show();
                    }
                    else if(e.getMessage().startsWith("An account already exists with the same email address but different sign-in credentials"))
                    {
                        Toast.makeText(getBaseContext(),getString(R.string.other_credential),Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                    googleSignInClient.signOut();
                }
            });
        } catch (ApiException e) {
            if(e.getMessage().contains("12500:"))
            {
                Intent intent=new Intent(getBaseContext(),update_play_services.class);
                startActivity(intent);
            }
            else if(e.getMessage().contains("7:"))
            {
                Toast.makeText(getBaseContext(),getString(R.string.noConnection),Toast.LENGTH_LONG).show();
            }
            else Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
        }
    }

    if(data!=null)TwitterSignUp.onActivityResult(requestCode, resultCode, data);
    if(data!=null)twitterSignIn.onActivityResult(requestCode, resultCode, data);
}

我将fabric io的元数据放在清单文件中,如下所示:

<meta-data
        android:name="io.fabric.ApiKey"
        android:value="***************************" />

这是数据中的错误消息:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=140, result=-1, data=Intent { (has extras) }} to activity {arb.passwordmanager/arb.passwordmanager.Sign}: java.lang.NullPointerException
                                                                       at android.app.ActivityThread.deliverResults(ActivityThread.java:3433)
                                                                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476)
                                                                       at android.app.ActivityThread.access$1200(ActivityThread.java:157)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                       at android.os.Looper.loop(Looper.java:176)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                    Caused by: java.lang.NullPointerException
                                                                       at com.google.android.gms.internal.zzdza.zzb(Unknown Source)
                                                                       at com.google.android.gms.internal.zzdzh.zza(Unknown Source)
                                                                       at com.google.firebase.auth.FirebaseAuth.signInWithCredential(Unknown Source)
                                                                       at arb.passwordmanager.Sign$10.success(Sign.java:616)
                                                                       at com.twitter.sdk.android.core.identity.TwitterAuthClient$CallbackWrapper.success(TwitterAuthClient.java:238)
                                                                       at com.twitter.sdk.android.core.identity.AuthHandler.handleOnActivityResult(AuthHandler.java:92)
                                                                       at com.twitter.sdk.android.core.identity.TwitterAuthClient.onActivityResult(TwitterAuthClient.java:171)
                                                                       at com.twitter.sdk.android.core.identity.TwitterLoginButton.onActivityResult(TwitterLoginButton.java:130)
                                                                       at arb.passwordmanager.Sign.onActivityResult(Sign.java:774)
                                                                       at android.app.Activity.dispatchActivityResult(Activity.java:5515)
                                                                       at android.app.ActivityThread.deliverResults(ActivityThread.java:3429)
                                                                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476) 
                                                                       at android.app.ActivityThread.access$1200(ActivityThread.java:157) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                       at android.os.Looper.loop(Looper.java:176) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5317) 
                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                       at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                       at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:1)

您正在测试的设备或模拟器没有安装与Firebase SDK 11.8.0兼容的Google Play服务版本。在应用程序初始化时,logcat将包含以下消息:

W/GooglePlayServicesUtil: Google Play services out of date

如果真实设备上发生错误,您需要更新设备上的Google Play服务。如果模拟器上发生错误,您需要使用SDK管理器下载最新的模拟器图像,然后选择包含Google API的图像。

由于Firebase需要Google Play服务,因此最好按照GoogleApiAvailability中的说明使用the documentation确认其可用。