Activity_A:
Intent intent = new Intent(Activity_A.this,
Activity_B.class);
intent.putExtra("user_lat",
getSharedPreferences(
Activity_C.USER_PREFS, 0).getFloat(
"lat", 0));
Log.d("lat from prefs = ",
getSharedPreferences(Activity_C.USER_PREFS, 0)
.getFloat("lat", 0));
intent.putExtra("lng",
getSharedPreferences(
Activity_C.USER_PREFS, 0).getFloat(
"user_lng", 0));
Log.d("lng from prefs = ",
getSharedPreferences(Activity_C.USER_PREFS, 0)
.getFloat("lng", 0)");
Logcatは次のことを示しています。
12-01 12:36:53.409:設定からのD / lat =(554):18.599348
12-01 12:36:53.409:設定からのD / lng =(554):73.7625
およびActivity_B:
try {
MyLat = this.getIntent().getDoubleExtra("user_lat", 0);
MyLng = this.getIntent().getDoubleExtra("user_lng", 0);
} catch (Exception e) {
Log.e("Exception in getting user lat and lng as Double",
e.toString());
}
if (MyLat == 0.0 || MyLng == 0.0) {
try {
MyLat = this.getIntent().getFloatExtra("user_lat", 0);
MyLng = this.getIntent().getFloatExtra("user_lng", 0);
} catch (Exception e) {
Log.e("Exception in getting user lat and lng as Float",
e.toString());
}
}
Log.d("MyLat and MyLng", MyLat + "+" + MyLng);
およびLogCat:
12-01 12:36:57.168:D / MyLatおよびMyLng(554):18.599348068237305 + 0.0
およびLogCatエラー:
12-01 12:36:57.138: W/Bundle(554): Key user_lat expected Double but value was a java.lang.Float. The default value 0.0 was returned.
12-01 12:36:57.159: W/Bundle(554): Attempt to cast generated internal exception:
12-01 12:36:57.159: W/Bundle(554): java.lang.ClassCastException: java.lang.Float
12-01 12:36:57.159: W/Bundle(554): at android.os.Bundle.getDouble(Bundle.java:1017)
12-01 12:36:57.159: W/Bundle(554): at android.content.Intent.getDoubleExtra(Intent.java:3377)
12-01 12:36:57.159: W/Bundle(554): at com.wicfy.mobileapp.MapMarkerActivity.onCreate(MapMarkerActivity.java:60)
12-01 12:36:57.159: W/Bundle(554): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-01 12:36:57.159: W/Bundle(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-01 12:36:57.159: W/Bundle(554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-01 12:36:57.159: W/Bundle(554): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-01 12:36:57.159: W/Bundle(554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-01 12:36:57.159: W/Bundle(554): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 12:36:57.159: W/Bundle(554): at android.os.Looper.loop(Looper.java:130)
12-01 12:36:57.159: W/Bundle(554): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-01 12:36:57.159: W/Bundle(554): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 12:36:57.159: W/Bundle(554): at java.lang.reflect.Method.invoke(Method.java:507)
12-01 12:36:57.159: W/Bundle(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-01 12:36:57.159: W/Bundle(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-01 12:36:57.159: W/Bundle(554): at dalvik.system.NativeStart.main(Native Method)
私は、 Activity_Bでuser_latをdoubleとして使用しているので、 intent.putExtra中にdoubleにキャストする必要があると思いました。
しかし、私はこれをLogCatで取得します。
12-01 12:36:57.168:D / MyLatおよびMyLng(554):0.0 + 0.0
12-01 12:07:50.218: W/Bundle(498): Key user_lat expected Float but value was a java.lang.Double. The default value 0.0 was returned.
12-01 12:07:50.249: W/Bundle(498): Attempt to cast generated internal exception:
12-01 12:07:50.249: W/Bundle(498): java.lang.ClassCastException: java.lang.Double
12-01 12:07:50.249: W/Bundle(498): at android.os.Bundle.getFloat(Bundle.java:984)
12-01 12:07:50.249: W/Bundle(498): at android.content.Intent.getFloatExtra(Intent.java:3360)
12-01 12:07:50.249: W/Bundle(498): at com.wicfy.mobileapp.MapMarkerActivity.onCreate(MapMarkerActivity.java:71)
12-01 12:07:50.249: W/Bundle(498): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-01 12:07:50.249: W/Bundle(498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-01 12:07:50.249: W/Bundle(498): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-01 12:07:50.249: W/Bundle(498): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-01 12:07:50.249: W/Bundle(498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-01 12:07:50.249: W/Bundle(498): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 12:07:50.249: W/Bundle(498): at android.os.Looper.loop(Looper.java:130)
12-01 12:07:50.249: W/Bundle(498): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-01 12:07:50.249: W/Bundle(498): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 12:07:50.249: W/Bundle(498): at java.lang.reflect.Method.invoke(Method.java:507)
12-01 12:07:50.249: W/Bundle(498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-01 12:07:50.249: W/Bundle(498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-01 12:07:50.249: W/Bundle(498): at dalvik.system.NativeStart.main(Native Method)
Log.eメッセージが表示されません。内部例外であり、クラッシュを引き起こさないことが原因である可能性があります。
では、 Activity_BのActivity_Aからインテントを介して送信している実際の値を取得するにはどうすればよいですか?
ありがとうございました