2014-10-08 55 views
0

我在運行時收到非法狀態異常。 Not connected.call connect() and wait for onConnected() to be called。以下我發佈了與此相關的代碼。然後我指出了該代碼中發生錯誤的位置。非法狀態exception.Not connected.call connect()並等待onConnected()被調用

堆棧跟蹤:

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

我不知道如何解決this.Anybody可以幫我this.Thank你。

+0

你能提供您LocLoader的代碼? – NickUnuchek 2014-11-10 21:01:58

回答

7

這裏問題在於你在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(); 
    } 

} 
+0

謝謝你。工作。 – Steve 2014-10-08 10:03:43

+0

它像魅力一樣工作。 +1 – KunalK 2015-07-28 09:16:00