java.lang.RuntimeException:由以下引起:java.lang.IllegalStateException:

时间:2017-08-22 05:45:07

标签: java android facebook firebase google-login

堆栈跟踪:

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)

我很确定GoogleApiClient导致此错误。那么让我解释一下究竟发生了什么。我使用片段显示4个登录选项(FB,Google,Phone)使用 firebase 和1个选项创建帐户使用电子邮件,这是另一个片段中执行的单独操作。因此,对FB和Google登录的响应在相同的片段(LoginTypeFragment)中处理。在此错误之前,我得到了:

Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet

通过在片段生命周期中管理GoogleApiClient连接/断开来解决。但是现在有些用户在每次更新时都遇到上述问题,此错误始终显示在 Google Play崩溃报告中。

以下是 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();
}

在FB.Google,电话,电子邮件按钮上执行操作点击:

 @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;
    }
}

关于活动结果:

 @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);}
}

firebase登录操作的OnActivityResult方法:

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

        }
    }
}

在firebase登录操作完成后,通过调用LoginTypeFragment的接口方法(OnHandleSucces)完成返回响应:

 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();
        }
    });
}

这是 LoginTypeFragment 的OnHandleSuccess方法:

 @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);

        });
    }
}

这是GoogleApiClient的代码(连接/断开连接):

@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();
}

所以,我已经解释了我在做什么以及发生了什么。请帮我解决问题。

2 个答案:

答案 0 :(得分:2)

所以我已经解决了这个问题。问题是在LoginActivity和CreateWithEmailFragment中初始化GoogleApiClient而没有断开LoginActivity的GoogleApiClient。所以我在LoginActivity中设置了GoogleApiClient,并且所有片段都访问了这个googleApiClient对象,而不是创建新的。

答案 1 :(得分:1)

检查GoogleApiClient.ConnectionCallbacks。如果您的onConnected方法被调用,请检查。

 @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(TAG, "GoogleApiClient: Connected");
    }