Stephen
Stephen

Reputation: 10079

Illegal state exception.Not connected.call connect() and wait for onConnected() to be called

I am getting a illegal state exception at Runtime.Not connected.call connect() and wait for onConnected() to be called.Below I am posted the codes related to that.And then I am pointed out the line exactly where the errors are occurred in this coding.

Stacktrace:

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

I didn't know how to solve this.Anybody can help me with this.Thank You.

Upvotes: 0

Views: 2479

Answers (1)

Sunny Shah
Sunny Shah

Reputation: 928

Here The Problem is that you are calling method on LocationClient while it is not connected to the play services.

replace your getNearMeLocation() method with the following:

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

}

Upvotes: 8

Related Questions