Reputation: 43
I am creating an app that requires signing in with a Twitter account as an authentication method. Then it links the Twitter account to Firebase.
I think the signing works fine but there is a problem with linking with Firebase, like some data are not retrieved properly which causes the NullPointException.
My activity has two Twitter signing buttons with different names.
This is my 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);
and this is the function that sets the callback to the Twitter sign in buttons:
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);
}
and this is the onActivityResult()
function, only the last two lines are important:
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);
}
and I put the meta data for the fabric io in the manifest file like the following:
<meta-data
android:name="io.fabric.ApiKey"
android:value="***************************" />
this is the error message in datails:
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)
Upvotes: 2
Views: 115
Reputation: 38289
The device or emulator you are testing with does not have a version of Google Play services installed that is compatible with Firebase SDK 11.8.0. At the time the app initializes the logcat will contain this message:
W/GooglePlayServicesUtil: Google Play services out of date
If the error occurs on a real device, you need to update Google Play services on the device. If the error occurs on an emulator, you need to use the SDK manager to download the latest emulator images and pick one the includes Google API.
Because Firebase requires Google Play services, it's good practice to confirm that it is available using GoogleApiAvailability as explained in the documentation.
Upvotes: 1