堆栈跟踪:

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个使用email创建帐户选项,这是在另一个片段中执行的单独操作。所以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);

        });
    }
}

这里是googleapclient(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();
}

所以,我已经解释了我在做什么和发生了什么。请帮我找出问题谢谢。

最佳答案

所以我已经解决了这个问题。问题是在不断开LoginActivity的GoogleApiclient的情况下,在LoginActivity和CreateWithEmailFragment中初始化GoogleApiclient。所以我在loginactivity中设置了googleapiclient,所有片段都访问这个googleapiclient对象,而不是创建新的。

关于java - java.lang.RuntimeException:原因:java.lang.IllegalStateException:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45809710/

10-09 05:49