Reputation: 24012
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)");
The Logcat shows:
12-01 12:36:53.409: D/lat from prefs =(554): 18.599348
12-01 12:36:53.409: D/lng from prefs =(554): 73.7625
and 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);
and LogCat:
12-01 12:36:57.168: D/MyLat and MyLng(554): 18.599348068237305+0.0
and the LogCat error:
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)
I thought, since i am taking user_lat as a double in Activity_B, i must cast it to double during intent.putExtra
but then i get this in LogCat:
12-01 12:36:57.168: D/MyLat and 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)
I get none of the Log.e message. May be because its an Internal Exception and doesn't cause a crash.
So, how can i get the actual values which i am sending throught intent from Activity_A in Activity_B
Thank You
Upvotes: 0
Views: 2631
Reputation: 86958
Since the value is already stored in a SharedPreferences file, it seems the simplest answer is to not use the Intent.
In Activity B use:
float myLat = getSharedPreferences(Activity_C.USER_PREFS, 0).getFloat("lat", 0);
// Read the Longitude in the same way
You are trying to work with Doubles from a Location object (I assume) however SharedPreferences don't work with Doubles. You might be safer converting the Doubles to Strings and back. This way you don't gain or lose any precision going to and from Floats.
Upvotes: 2
Reputation: 40218
While for primitives you can cast double
to a float
(with loss of precision) and float
to a double
(with no warnings), in case of Float
and Double
this won't work. So every time you put a Float
into a Bundle
you must be sure it's a Float
and not a Double
. Hope this helps.
Upvotes: 2