This is the code on the link https://developer.android.com/training/location/retrieve-current.html although it is the same code I got error. I got the permission on manifest file. Thank you...
public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, OnClickListener{
// Global constants
/*
* Define a request code to send to Google Play services
* This code is returned in Activity.onActivityResult
*/
Location mCurrentLocation;
private final static int
CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this, this, this);
}
// Define a DialogFragment that displays the error dialog
public static class ErrorDialogFragment extends DialogFragment {
// Global field to contain the error dialog
private Dialog mDialog;
// Default constructor. Sets the dialog field to null
public ErrorDialogFragment() {
super();
mDialog = null;
}
// Set the dialog to display
public void setDialog(Dialog dialog) {
mDialog = dialog;
}
// Return a Dialog to the DialogFragment.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialog;
}
}
/*
* Handle results returned to the FragmentActivity
* by Google Play services
*/
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST :
/*
* If the result code is Activity.RESULT_OK, try
* to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK :
/*
* Try the request again
*/
break;
}
}
}
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode =
GooglePlayServicesUtil.
isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("Location Updates",
"Google Play services is available.");
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Get the error code
// Get the error dialog from Google Play services
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
resultCode,
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment =
new ErrorDialogFragment();
// Set the dialog in the DialogFragment
errorFragment.setDialog(errorDialog);
// Show the error dialog in the DialogFragment
errorFragment.show(getSupportFragmentManager(),
"Location Updates");
}
}
return false;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// TODO Auto-generated method stub
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
showDialog(connectionResult.getErrorCode());
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
/*
* Called when the Activity becomes visible.
*/
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mLocationClient.connect();
}
/*
* Called when the Activity is no longer visible.
*/
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mLocationClient.disconnect();
super.onStop();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
mCurrentLocation = mLocationClient.getLastLocation();
tv.setText(mCurrentLocation.getLatitude()+"");
}
}
And The Error I got is below here
09-18 15:11:43.660: E/AndroidRuntime(6839): FATAL EXCEPTION: main
09-18 15:11:43.660: E/AndroidRuntime(6839): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testapplication/com.example.testapplication.MainActivity}: java.lang.ClassNotFoundException: com.example.testapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.testapplication-1.apk]
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.os.Looper.loop(Looper.java:130)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread.main(ActivityThread.java:3693)
09-18 15:11:43.660: E/AndroidRuntime(6839): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:11:43.660: E/AndroidRuntime(6839): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 15:11:43.660: E/AndroidRuntime(6839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-18 15:11:43.660: E/AndroidRuntime(6839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-18 15:11:43.660: E/AndroidRuntime(6839): at dalvik.system.NativeStart.main(Native Method)
09-18 15:11:43.660: E/AndroidRuntime(6839): Caused by: java.lang.ClassNotFoundException: com.example.testapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.testapplication-1.apk]
09-18 15:11:43.660: E/AndroidRuntime(6839): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-18 15:11:43.660: E/AndroidRuntime(6839): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-18 15:11:43.660: E/AndroidRuntime(6839): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-18 15:11:43.660: E/AndroidRuntime(6839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
09-18 15:11:43.660: E/AndroidRuntime(6839): ... 11 more