如何在Fragments中使用GoogleAPI Client

时间:2017-05-30 06:10:28

标签: java android android-fragments android-fragmentactivity android-googleapiclient

我无法在Fragment中使用Google API客户端。

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_case_summary, container, false);

        initView(rootView);
        return rootView;

      if(mGoogleApiClient==null) {
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
         }

它显示错误: enter image description here

1 个答案:

答案 0 :(得分:0)

只需切换return语句和google API客户端初始化:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.layout_case_summary, container, false);

    initView(rootView);

    if(mGoogleApiClient==null) {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    return rootView;
}

return语句应始终是方法中的最后一行。