mGoogleApiClient.isConnected()返回null [英] mGoogleApiClient.isConnected() returns null

查看:63
本文介绍了mGoogleApiClient.isConnected()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了所有StackOverflow问题,并尝试了每种方法,但问题仍然存在.

I have gone through all the StackOverflow questions and tried every method but still the problem persists.

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

After building when I print the toast it gives false everytime, what could be the error? I am following this tutorial and want to retrieve the name of my current place. http://code.tutsplus.com/articles/google-play-services-using-the-places-api--cms-23715

推荐答案

问题出在mGoogleApiClient.isConnected()上,它为您提供了错误信息,这就是为什么它无法执行进一步的工作.如果仅删除if(mGoogleApiClient.isConnected())部分,它将起作用.

The Problem is with mGoogleApiClient.isConnected(), it gives you false and that's why it is unable to perform the further working. If you just remove the if(mGoogleApiClient.isConnected()) part, it will work.

这样做:

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

然后在mGoogleApiClient .... build();

And call the showPlaces method, just after mGoogleApiClient....build();

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

showPlaces();

这篇关于mGoogleApiClient.isConnected()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆