预期Float但值是java.lang.Double

时间:2012-12-01 07:14:17

标签: android floating-point double

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:来自prefs的D / lat =(554):18.599348

12-01 12:36:53.409:来自prefs的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 作为双倍,我必须在 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 获取我通过意图发送的实际值

谢谢

2 个答案:

答案 0 :(得分:2)

对于基元,您可以将double转换为float(精度不足)和float转换为double(没有警告),如果是{ {1}}和Float这不起作用。因此,每次将Double放入Float时,您必须确定它是Bundle而不是Float。希望这会有所帮助。

答案 1 :(得分:2)

由于该值已存储在SharedPreferences文件中,因此最简单的答案似乎是不使用Intent。

在活动B中使用:

float myLat = getSharedPreferences(Activity_C.USER_PREFS, 0).getFloat("lat", 0);
// Read the Longitude in the same way

你正在尝试使用Location对象中的双打(我假设)但是SharedPreferences不适用于双打。你可能会更安全地将双打转换为字符串并返回。通过这种方式,您不会获得或失去往返Floats的精确度。