融合された位置プロバイダーを使用して現在の位置座標を取得する小さなモジュールを作成しました。
私のMainActivity.javaファイルは次のようになります
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()));
}
}
モバイルでアプリを実行しようとすると、次のエラーが表示されます: 残念ながら、Module01 が停止しました。Logcatは次 のようになります
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)
この問題を解決するにはどうすればよいですか?