0

活动_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

和活动_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期间将其转换为双倍

但后来我在 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获取我通过意图发送的实际值

谢谢你

4

2 回答 2

2

虽然对于原语,您可以强制double转换为 a float(精度损失)和floata (没有警告) ,double以防万一这不起作用。因此,每次将 a放入 a时,都必须确保它是 a而不是 a 。希望这可以帮助。FloatDoubleFloatBundleFloatDouble

于 2012-12-01T07:24:50.803 回答
2

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

在活动 B 中使用:

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

您正在尝试使用 Location 对象中的 Doubles(我假设),但是 SharedPreferences 不适用于 Doubles。将 Doubles 转换为 Strings 并返回可能会更安全。这样您就不会获得或失去任何往返浮动的精度。

于 2012-12-01T07:26:11.023 回答