I ve made a small module to get current location coordinates using fused location provider.
My MainActivity.java file looks like this
TextView globalCoordinates = null;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
globalCoordinates = (TextView)findViewById(R.id.textView);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) getApplicationContext())
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener)getApplicationContext())
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
globalCoordinates.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude())+ ""
+ "Longitude: " + String.valueOf(mLastLocation.getLongitude()));
}
}
When I try to run the app on my mobile it gives the following error: Unfortunately, Module01 has stopped. The Logcat looks like this
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atifarain.module01/com.example.atifarain.module01.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
at com.example.atifarain.module01.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
How can I resolve this issue?