2013-12-07 3 views
1

Мне нужна помощь с моим приложением. Я пытался создать приложение для простого местоположения. Поэтому я следил за обучением разработчиков Android (http://developer.android.com/training/location/retrieve-current.html). Тем не менее, я застрял с последней частью. Мое приложение, кажется, аварии, когда он попадает этот код,Android Location-Based App

mLocationClient.connect(); 

Я добавил разрешение и активированную услугу определения местоположения в устройстве. Может кто-нибудь мне помочь?

Это - весь мой код.

public class MainActivity extends FragmentActivity 
implements 
ConnectionCallbacks, 
OnConnectionFailedListener{ 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

private LocationClient mLocationClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mLocationClient = new LocationClient(this, this, this); 
} 

@Override 
protected void onStart(){ 
    super.onStart(); 
    mLocationClient.connect(); 
} 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public static class ErrorDialogFragment extends DialogFragment{ 
    private Dialog mDialog; 
    public ErrorDialogFragment(){ 
     super(); 
     mDialog = null; 
    } 
    public void setDialog(Dialog dialog){ 
     mDialog = dialog; 
    } 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 
     return mDialog; 
    } 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    switch (requestCode) { 
     case CONNECTION_FAILURE_RESOLUTION_REQUEST : 

      switch (resultCode) { 
       case Activity.RESULT_OK: 
       break; 
       default: 
       break; 
      } 
     default: 
      break; 
    } 
} 

private boolean servicesConnected() { 
    int resultCode = 
      GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (ConnectionResult.SUCCESS == resultCode) { 
     Log.d("Location Update", "Google Play Services is available"); 
     return true; 
    } else { 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0); 
     if (dialog != null) { 
      //ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
      //errorFragment.setDialog(dialog); 
      //errorFragment.show(getFragmentManager(), "Location Updates"); 
      //errorFragment.show(getSupportFragmentManager(), "Location Updates"); 
     } 
     return false; 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    // TODO Auto-generated method stub 
    if (connectionResult.hasResolution()) { 
     try { 

      connectionResult.startResolutionForResult(
        this, 
        CONNECTION_FAILURE_RESOLUTION_REQUEST); 

     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     showErrorDialog(connectionResult.getErrorCode()); 
    } 
} 

private void showErrorDialog(int errorCode) { 
    Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
     errorCode, 
     this, 
     CONNECTION_FAILURE_RESOLUTION_REQUEST); 
    if (errorDialog != null) { 
     ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
     errorFragment.setDialog(errorDialog); 
    } 
} 

@Override 
public void onConnected(Bundle arg0) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 

} 

@Override 
public void onDisconnected() { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); 
} 

} 
+0

Какая ошибка возникает при сбое приложения? –

ответ

2

Я думаю, что у вас нет установленных сервисов Google Play. У вас есть метод servicesConnected(), но вы его не используете. Вы должны перенести свой вызов на mLocationClient.connect() в оператор if, который проверяет servicesConnected().

1

У меня была аналогичная проблема, и я забыл добавить это в моем манифесте заявочный:

<meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

Read this снова на помощь.