我的应用程序需要帮助。 我一直在努力建立一个简单的位置应用程序。因此,我遵循Android开发人员培训(http://developer.android.com/training/location/retrieve-current.html)。但是,我坚持最后一部分。 我的应用程序似乎在遇到此代码时崩溃,
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(),但您不使用它。您应该在检查servicesConnected()的if语句中将您的调用包装到mLocationClient.connect()。
答案 1 :(得分:1)
我有一个类似的问题,我忘记在应用程序中的清单中添加它:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
再次阅读this寻求帮助。