0

我正在为 android 编写一个应用程序,该应用程序应该从 JSON 文档中获取坐标,将此字符串转换为双精度,然后使用此双精度在谷歌地图上创建叠加层。

但我得到一个名为 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();
    }

}

    }

这是我的日志:

    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

有人有什么想法吗?logcat 告诉我我有来自 JSON 的数据。

4

3 回答 3

1

例外是告诉您到底出了什么问题:

NumberFormatException:无效的双精度:“”

您正在尝试Double.parseDouble在 string 上使用""。那失败了。您需要检查""案例:

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

于 2013-01-12T11:30:38.090 回答
0

看起来您没有检查(null)条件,在这里您解析为 null 的 String 值,异常显示它无法将 null String 解析为 Double ..所以,您必须在解析之前检查null条件加倍..检查一下,您的问题就解决了...享受..:-)

于 2013-01-12T11:10:24.493 回答
0

您需要处理空的 lat 值。我看到 JSON 返回中有这样的数据:

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

所以这段代码抛出异常:

double lati = Double.parseDouble(lat);
于 2013-01-12T11:13:26.467 回答