mGoogleApiClient.isConnected()返回null

时间:2015-05-20 17:34:17

标签: android google-play-services google-api-client

我已经完成了所有StackOverflow问题并尝试了所有方法,但问题仍然存在。

public class PlaceInfoActivity extends ActionBarActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static final String LOG_TAG = "PlaceInfoActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private GoogleApiClient mGoogleApiClient;
TextView mTextView;
String content = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_place_info);

    mTextView = (TextView) findViewById(R.id.textView0);

    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .enableAutoManage(this, 0, this)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    Toast.makeText(this, Boolean.toString(mGoogleApiClient.isConnected()), Toast.LENGTH_SHORT).show();


    if (mGoogleApiClient.isConnected()) {

        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                PlaceLikelihood placeLikelihood = likelyPlaces.get(0);

                if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName()))
                    content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
                if (placeLikelihood != null)
                    content += "Percent change of being there: " + (int) (placeLikelihood.getLikelihood() * 100) + "%";
                mTextView.setText(content);

                likelyPlaces.release();
            }
        });
    }
}

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

@Override
protected void onResume() {
    super.onResume();

    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

} }

在我打印吐司后建立它每次都给出错误,可能是错误?我正在按照本教程想要检索当前位置的名称。 http://code.tutsplus.com/articles/google-play-services-using-the-places-api--cms-23715

2 个答案:

答案 0 :(得分:0)

它始终为假,因为您致电connect() onStart。声明方法

public void showPlaces() {
      PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                PlaceLikelihood placeLikelihood = likelyPlaces.get(0);

                if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName()))
                    content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
                if (placeLikelihood != null)
                    content += "Percent change of being there: " + (int) (placeLikelihood.getLikelihood() * 100) + "%";
                mTextView.setText(content);

                likelyPlaces.release();
            }
        });
}

并在调用onConnected时调用它

@Override
public void onConnected(Bundle bundle) {
  showPlaces();
}

答案 1 :(得分:0)

问题出在mGoogleApiClient.isConnected()上,它会给你错误,以及为什么它无法执行进一步的工作。如果您只删除if(mGoogleApiClient.isConnected())部分,它将起作用。

这样做:

PRINT

在mGoogleApiClient .... build()之后调用showPlaces方法;

    public void showPlaces() {

    PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            StringBuffer sb=new StringBuffer();
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                String name, per;
                name = placeLikelihood.getPlace().getName().toString()+"      ";
                per = ""+(int) (placeLikelihood.getLikelihood() * 100)+"\n";
                sb.append(name+per);
            }
            content = sb.toString();
            mTextView.setText(content);
            likelyPlaces.release();
        }
    });
}