2013-01-12 68 views
0

我正在編寫一個應用程序的android應該從JSON文件獲取座標,將此字符串轉換爲double,然後使用此雙打在Google地圖上創建疊加層。在Android和JSON中解析時NumberFormatException。字符串 - >雙

但我得到一個名爲NumberFormatException的異常。

public class LagerActivity extends Activity { 

private static String url = "http://agent.nocrew.org/api/json/1.0/searchStore.json?limit=500"; 

static final String TAG_ITEMS = "items"; 
static final String TAG_LAT = "lat"; 
static final String TAG_LNG = "lng"; 

private GoogleMap mMap; 
private LocationManager locationManager; 

JSONArray items = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_lager); 
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
      .getMap(); 

    // Creating JSON Parser instance 
    JSONparser jParser = new JSONparser(); 
    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     // Getting Array of Contacts 
     items = json.getJSONArray(TAG_ITEMS); 
     // looping through All Contacts 
     for (int i = 0; i < items.length(); i++) { 
      JSONObject c = items.getJSONObject(i); 

      // Storing each json item in variable 
      String lat = c.getString(TAG_LAT); 
      String lng = c.getString(TAG_LNG); 

      // lat = lat.replace(",", "."); 
      // lng = lng.replace(",", "."); 

      // Log.e(lat, lng); 

      double lati = Double.parseDouble(lat); 

      Log.d(Double.toString(lati), " "); 

      // LatLng point = new LatLng(Double.parseDouble(lat), 
      // Double.parseDouble(lng)); 
       // mMap.addMarker(new MarkerOptions().position(point) 
      // .title("Karls räkor & hund") 
      // .snippet("blaha, ehehe")); 

     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

    } 

這裏是我的logcat:

01-11 18:10:23.294: W/dalvikvm(22718): threadid=1: thread exiting with uncaught   exception (group=0x40e86930) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): FATAL EXCEPTION: main 
    01-11 18:10:23.294: E/AndroidRuntime(22718): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.example.checkmypint/com.example.checkmypint.LagerActivity}: java.lang.NumberFormatException: Invalid double: "" 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.os.Handler.dispatchMessage(Handler.java:99) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.os.Looper.loop(Looper.java:137) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.ActivityThread.main(ActivityThread.java:5039) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at java.lang.reflect.Method.invokeNative(Native Method) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at java.lang.reflect.Method.invoke(Method.java:511) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at dalvik.system.NativeStart.main(Native Method) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): Caused by: java.lang.NumberFormatException: Invalid double: "" 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at java.lang.StringToReal.parseDouble(StringToReal.java:248) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at java.lang.Double.parseDouble(Double.java:295) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at com.example.checkmypint.LagerActivity.onCreate(LagerActivity.java:65) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.Activity.performCreate(Activity.java:5104) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
    01-11 18:10:23.294: E/AndroidRuntime(22718): ... 11 more 

Annyone了ANNY想法? logcat告訴我我有來自JSON的數據。

+2

'無效雙:「」'你的JSON有一個空字符串,這不是一個有效的解析雙。 –

回答

1

唯一的例外是在告訴你正是什麼是錯的:

NumberFormatException: Invalid double: ""

你試圖在字符串""上使用Double.parseDouble。那失敗了。你需要檢查的""情況:

String lat = c.getString(TAG_LAT); 
double lati; 

if (lat == null || lat.length == 0) { 
    lati = /* use whatever default you want to use here */; 
} 
else { 
    double lati = Double.parseDouble(lat); 
} 

...同樣地,對於lng,大概。

你想趕上,並在一定程度上處理異常反正(如果傳入的JSON是真正的無效,lat: "foo"或類似),但由於明顯的輸入數據有時有空白latlng,這是沒有更長的例外情況,因此您可以使用if以內聯方式處理它。

+0

感謝分配,這沒有把戲! – user1972221

0

它看起來像你沒有檢查(空)條件,在這裏你解析字符串值爲空,並且異常顯示它不能解析空字符串爲雙倍..所以,你必須檢查null條件之前解析它爲雙倍..檢查並解決您的問題...享受.. :-)

+0

我同時顯示這兩種方式..或者「」或檢查null ..dear ... :-) –

+0

@ T.J.Crowder ..我編輯了我的答案任何misguide .. –

0

您需要處理空的緯度值。我看到有在JSON回報這樣的數據:

{ 
    "address": "Breared, Vrångagatan 5", 
    "city": "Varberg", 
    "county": "Halland", 
    "county_id": 3, 
    "lat": "", 
    "lng": "", 
    "id": 1331, 
    "sysid": 1310 
}, 

所以這個代碼拋出異常:

double lati = Double.parseDouble(lat);