非法状态异常。没有connected.call connect()并等待onConnected()被调用

时间:2014-10-08 09:48:11

标签: android illegalstateexception

我在运行时获得了一个非法的状态异常。Not connected.call connect() and wait for onConnected() to be called。下面我发布了与之相关的代码。然后我指出了该编码中出现错误的确切位置。

堆栈跟踪:

 E/AndroidRuntime(1305): FATAL EXCEPTION: main
 E/AndroidRuntime(1305): Process: com.steph.example, PID: 1305
 E/AndroidRuntime(1305): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
 E/AndroidRuntime(1305):    at com.google.android.gms.internal.hb.cn(Unknown Source)
 E/AndroidRuntime(1305):    at com.google.android.gms.internal.jg.b(Unknown Source)
 E/AndroidRuntime(1305):    at com.google.android.gms.internal.jg$c.cn(Unknown Source)
 E/AndroidRuntime(1305):    at com.google.android.gms.internal.jf.getLastLocation(Unknown Source)
 E/AndroidRuntime(1305):    at com.google.android.gms.internal.jg.getLastLocation(Unknown Source)
 E/AndroidRuntime(1305):    at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
 E/AndroidRuntime(1305):    at com.steph.example.Main.getNearMeLocation(Main.java:264)
 E/AndroidRuntime(1305):    at com.steph.example.Main.onOptionsItemSelected(Main.java:183)
 E/AndroidRuntime(1305):    at android.app.Activity.onMenuItemSelected(Activity.java:2600)
 E/AndroidRuntime(1305):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
 E/AndroidRuntime(1305):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)

Main.java:

public class Main extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

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

    if(usr.getUserID()!=0){
            displayView(2);
        }else{
            displayView(0);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.connect();
        if(usr.getUserID()!=0)
            loadSubscriptionDetails();
    }

    @Override
    protected void onStop() {
        mLocationClient.disconnect();
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        switch (item.getItemId()) {
        case R.id.action_nearme:
            getNearMeLocation(); ----->183 Line
            return true;
        case R.id.action_register:
            Intent intent = new Intent(this, Login.class);
            startActivity(intent);
            return true;
        case R.id.action_settings:
            return true;
        case R.id.action_feedback:
            return true;
        case R.id.action_help:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    public void getNearMeLocation() {
        Location currentLocation = mLocationClient.getLastLocation(); ----> 264th line
        if (currentLocation != null) {
            (new LocLoader(getApplicationContext())).execute(currentLocation);
        } else {
            Toast.makeText(getApplicationContext(), "Location not available...", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if(usr.getUserID()==0)
            getNearMeLocation();
    }

    @Override
    public void onDisconnected() {
    }
}

我不知道如何解决这个问题。任何人都可以帮助我。感谢你。

1 个答案:

答案 0 :(得分:8)

这里的问题是你在没有连接到播放服务的情况下在LocationClient上调用方法。

使用以下内容替换您的getNearMeLocation()方法:

public void getNearMeLocation() {
if(mLocationClient.isConnected()){
    Location currentLocation = mLocationClient.getLastLocation(); ----> 264th line
    if (currentLocation != null) {
        (new LocLoader(getApplicationContext())).execute(currentLocation);
    } else {
        Toast.makeText(getApplicationContext(), "Location not available...", Toast.LENGTH_SHORT).show();
    }

}