使用TabLayout Viewpager时,获取“已经管理ID为0的GoogleApiClient”异常

时间:2017-02-09 13:29:46

标签: android android-fragments google-api-client

我想使用TabLayout ViewPager Fragments集成Google Signup。 ViewPager包含两个片段LoginFragment和RegistrationFragment,两者都包含“Google注册”按钮。

问题是我在启动活动时遇到以下错误。

  

java.lang.IllegalStateException:已经在管理GoogleApiClient   id为

完整的错误日志 -

java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
at com.google.android.gms.internal.zzzt.zza(Unknown Source)
at com.google.android.gms.common.api.GoogleApiClient$Builder.zzf(Unknown Source)
at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
at com.naturesouq_shopping.fragment.RegistrationFragment.buildGoogleApiClient(RegistrationFragment.java:174)
at com.naturesouq_shopping.fragment.RegistrationFragment.getIds(RegistrationFragment.java:140)
at com.naturesouq_shopping.fragment.RegistrationFragment.onCreateView(RegistrationFragment.java:85)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:20236)

我正在尝试关注代码

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

 // Configure sign-in to request the user's ID, email address, and basic
 // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        buildGoogleApiClient();

     // .....
 }

private synchronized void buildGoogleApiClient() {
    /** build_client */
   try {
       mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
               .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
               .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
               .build();
   } catch (Exception e) {
       e.printStackTrace();
   }
}

@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    } 

}

@Override
public void onStop() {
    Log.d("GARG", "***** on Stop ***** ");
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        Log.d("GARG", "***** on Stop mGoogleApiClient disconnect ***** ");

        mGoogleApiClient.stopAutoManage(getActivity());
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

还有其他方法可以连接Google API客户端吗? 我知道我做错了什么和正确的方法吗?

3 个答案:

答案 0 :(得分:12)

我认为您正在尝试通过已构建和连接的googleApiClient构建和连接。这可以解决您的问题:

if(mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
 try {
   mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
           .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
           .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
           .build();
 } catch (Exception e) {
   e.printStackTrace();
 }
}

答案 1 :(得分:6)

official doc for enableAutoManage说:

  

在任何给定时间,每个ID只允许一个自动管理的客户端。要重用id,必须先在前一个客户端上调用stopAutoManage(FragmentActivity)。

您的代码使用的是enableAutoManage版本而没有clientId参数,因此它默认为0.下面我解释为什么您将为clientId 0提供多个自动管理客户端,这就是上述文档警告的内容。

将一个片段附加到FragmentActivity后,它会告诉该活动开始管理GoogleApiClient的新实例。但是,如果FragmentActivity已经在管理另一个GoogleApiClient实例呢?那是你收到错误的时候。

在您的情况下,要求包含ViewPager的FragmentActivity管理两个不同的GoogleApiClients,一个来自RegistrationFragment,另一个来自LoginFragment。根据堆栈跟踪,FragmentActivity首先开始管理LoginFragment的GoogleApiClient,然后当您要求它管理RegistrationFragment的GoogleApiClient时崩溃。

你可以尝试一些事情:

  • 将两个不同的clientId传递给enableAutoManage,一个用于LoginFragment,另一个用于ReservationFragment。
  • 找出你需要调用stopAutoManage的位置。使用ViewPager中的Fragments可能会很棘手。
  • 如果我是你,我会认真考虑在包含ViewPager而不是RegisterFragment和LoginFragment的FragmentActivity中创建GoogleApiClient。那么你可以在Activity中完成需要GoogleApiClient的工作,或者在从Activity中获取GoogleApiClient之后继续在两个片段中执行此操作:

私有GoogleApiClient googleApiClient;

  @Override
  void onAttach(Activity activity) {
      super.onAttach(activity);
      googleApiClient = activity.getGoogleApiClient();
  }

  @Override
  void onDetach() {
      super.onDetach();
      googleApiClient = null;
  }

答案 2 :(得分:0)

只要改变 enableAutoManage(getActivity() , this)

致 enableAutoManage(getActivity(),1, this )