StackTrace:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2335)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2397)
at android.app.ActivityThread.access$800 (ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5268)
at java.lang.reflect.Method.invoke (Native Method)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:697)
Caused by: java.lang.IllegalStateException:
at com.google.android.gms.common.internal.zzbo.zza (Unknown Source)
at com.google.android.gms.internal.zzbat.zza (Unknown Source)
at com.google.android.gms.common.api.GoogleApiClient$Builder.build
(Unknown Source)
at com.plowns.droidapp.fragments.LoginTypeFragment.onCreate (Unknown
Source)
at android.support.v4.app.Fragment.performCreate (Unknown Source)
at android.support.v4.app.FragmentManagerImpl.moveToState (Unknown
Source)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut
(Unknown Source)
at android.support.v4.app.FragmentTransition.calculateFragments
(Unknown Source)
at android.support.v4.app.FragmentTransition.startTransitions (Unknown
Source)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether
(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps
(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.execPendingActions
(Unknown Source)
at android.support.v4.app.FragmentController.execPendingActions
(Unknown Source)
at android.support.v4.app.FragmentActivity.onStart (Unknown Source)
at android.support.v7.app.AppCompatActivity.onStart (Unknown Source)
at android.app.Instrumentation.callActivityOnStart
(Instrumentation.java:1236)
at android.app.Activity.performStart (Activity.java:6049)
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2298)
I am pretty sure this error caused by GoogleApiClient. So let me explain what exactly happening . I am using a fragment to show 4 options for login by (FB,Google,Phone) using firebase and 1 option for create account using email that is separate action performed in another fragment . So response for FB and Google login is handled in same fragment (LoginTypeFragment). Before this error i was getting :
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
that is resolved by managing GoogleApiClient connect/disconnect in fragment life cycle. But now some user facing above problem in every update this error always showing in Google Play Crash reports.
Here is code for LoginTypeFragment.java
private static final String TAG = "LoginTypeFragment";
private FirebaseLoginOperations loginOperations;
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private AppCompatButton btnEmail,btnFacebook,btnGoogle,btnPhone,btnNewAccEmail;
Context mContext;
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
loginOperations = new FirebaseLoginOperations(this,this,TAG);;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getActivity().getResources().getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity() , new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(getActivity(), "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
Action performed on FB.Google,Phone,Email button click:
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_login_w_email:
Utils.fbEventLog("Login_With_Email_Click","click","Email");
LoginWithEmailFragment loginWithEmailFragment = new LoginWithEmailFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right);
transaction.replace(R.id.fragment_container_login, loginWithEmailFragment, "LoginWithEmailFragment");
transaction.addToBackStack("LoginWithEmailFragment");
transaction.commit();
break;
case R.id.btn_facebook:
Utils.fbEventLog("Login_With_Facebook_Click","click","Facebook");
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
break;
case R.id.btn_google:
Utils.fbEventLog("Login_With_Google_Click","click","Google");
if(mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
break;
case R.id.btn_login_w_phone:
Utils.fbEventLog("Login_With_Phone_Click","click","Phone");
FragmentManager fragmentManager2 = getActivity().getSupportFragmentManager();
fragmentManager2.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right)
.replace(R.id.fragment_container_login, CreateWithPhoneFragment.newInstance(), "CreateWithPhoneFragment")
.addToBackStack("CreateWithPhoneFragment").commit();
break;
case R.id.btn_new_acc_with_email:
Utils.fbEventLog("Create_With_Email_Click","click","Email");
FragmentManager fragmentManager3 = getActivity().getSupportFragmentManager();
fragmentManager3.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right)
.replace(R.id.fragment_container_login, CreateWithEmailFragment.newInstance(), "CreateWithEmailFragment")
.addToBackStack("CreateWithEmailFragment").commit();
break;
}
}
On Activity Result:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
if(isAdded()&&isVisible()){
loginOperations.onActivityResult(requestCode, resultCode, data);}
}
OnActivityResult method of firebase login operation:
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Log.e(TAG,result.getStatus().getStatusCode()+"");
Log.e(TAG,result.getStatus().getStatusMessage()+"");
// Google Sign In failed, update UI appropriately
}
}
}
After firebase login operation complete returning response by calling interface method(OnHandleSucces) of LoginTypeFragment:
private void getTokenAndSave(){
FirebaseUserUtils.getUserToken(true, task1 -> {
if (task1.isSuccessful() && baseFragment.isAdded()) {
FirebaseUserUtils.saveUserToken(baseFragment.getActivity(), task1.getResult().getToken());
if(handle!=null) {
handle.onHandleSuccess();
}else{
baseFragment.hideProgressDialog();
}
}else{
// If sign in fails, display a message to the user.
String errorMessage= ParseNetworkErrors.parseFebError(baseFragment.getContext(),task1.getException());
Log.e(TAG, errorMessage);
Toast.makeText(baseFragment.getContext(),errorMessage,Toast.LENGTH_LONG).show();
baseFragment.hideProgressDialog();
}
});
}
And here is OnHandleSuccess method of LoginTypeFragment:
@Override
public void onHandleSuccess() {
if(isAdded()&&isVisible()) {
loginOperations.isUserAccountCreated(responseObj -> {
Bundle params = new Bundle();
if (responseObj) {
params.putString("result", "Success");
FirebaseUserUtils.pwnUserLoggedIn(getActivity(), true);
FirebaseUserUtils.getCurrentUser(getActivity());
Intent i = new Intent(getActivity(), HomeActivity.class);
startActivity(i);
getActivity().overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
getActivity().finish();
} else {
params.putString("result", "Account Incomplete");
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
AccountTypeFragment accountTypeFragment = AccountTypeFragment.newInstance(SignUpType.FB, "FB_Google");
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
transaction.replace(R.id.fragment_container_login, accountTypeFragment, "AccountTypeFragment");
transaction.addToBackStack("AccountTypeFragment");
transaction.commit();
}
mFirebaseAnalytics.logEvent("Login", params);
mFirebaseAnalytics.setAnalyticsCollectionEnabled(true);
});
}
}
And here is code for GoogleApiClient (connect/disconnect) :
@Override
public void onPause() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if(mGoogleApiClient!=null && !mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
}else if(mGoogleApiClient==null){
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getActivity().getResources().getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity() , new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(getActivity(), "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
@Override
public void onDetach() {
AppSingleton.getInstance(getActivity()).cancelPendingRequests(TAG);
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
super.onDetach();
}
@Override
public void onDestroy() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
@Override
public void onStop() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
super.onStop();
}
So, i have explained what i am doing and what happening . Please help me find out problem thanks.
2
Answers
So I have resolved this. The problem was the initialization of GoogleApiClient In LoginActivity and in CreateWithEmailFragment without disconnecting GoogleApiClient of LoginActivity. So I setup GoogleApiClient in LoginActivity and all fragment access this googleApiClient Object rather than creating new.
check you
GoogleApiClient.ConnectionCallbacks
. Check, if youronConnected
method called.