2012-12-01 26 views
0

Activity_A:預期浮動,但其值是一個java.lang.Double中

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月12日至1日:36:53.409:從首選項=(554)d/LAT :18.599348

12月12日至1日:36:53.409:d /從首選項=(554)LNG: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月12日至1日: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) 

我想,因爲我將user_lat作爲Activity_B中的雙精度值,所以我必須將它轉換爲期間intent.putExtra

但然後我得到這個在logcat中:

12月12日至1日: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發送throught意圖Activity_A實際值

謝謝

+0

我用一個建議更新了我的答案。 – Sam

回答

2

由於該值已存儲在SharedPreferences文件中,因此似乎最簡單的答案是不使用Intent。

在活動B使用:

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

您是試圖從一個位置對象(我認爲),但SharedPreferences不雙打工作,雙打的工作。你可能會更安全地將雙打轉換成絃樂,然後再回來。這樣你就不會獲得或失去進入和來自Floats的精確度。

2

雖然原語,你可以投doublefloat(有損失的精度)和floatdouble(沒有警告),在FloatDouble的情況下,這將不起作用。因此,每當您將Float放入Bundle時,您都必須確定它是Float而不是Double。希望這可以幫助。

相關問題