Android Studio 无法将String转换为double

uemypmqf  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(365)

所以我得到了纬度和经度,并成功地将其保存在firebase上,现在我能够检索它并将它们的值粘贴到TextView上。
我计划从TextView中获取字符串值,并将其转换为双精度值,然后将其用作纬度和经度来显示Map上的位置。我遵循了将字符串值转换为双精度值的格式,但它仍然不起作用。
这是我的代码:
@覆盖公共void onMapReady(谷歌Map谷歌Map){ mMap =谷歌Map;

String latitude = textViewLatt.getText().toString();
    String longitude = textViewLong.getText().toString();

    //Double latitude = Double.valueOf(textViewLatt.getText().toString());
    //Double longitude = Double.valueOf(textViewLong.getText().toString());

    double latt = Double.parseDouble(latitude);
    double longi= Double.parseDouble(longitude);

    //double latt = Double.valueOf(latitude);
    //double longi = Double.valueOf(longitude);

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(latt, longi);
    mMap.addMarker(new MarkerOptions()
            .position(sydney)
            .title("Marker"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

这些都是我在调试中得到的错误,我试着搜索问题,但我找不到任何解决方案。任何帮助都是很好的。
enter image description here
enter image description here

cbeh67ev

cbeh67ev1#

检查您的文本视图,您不知何故未能在文本视图中正确设置LatLong值的数字,它们包含字母,这就是为什么您无法将它们解析为Double。

ni65a41a

ni65a41a2#

更新:我发现了这个问题,它似乎在我将文本视图的值改为数字之前就已经得到了值。因此,它试图将“纬度”(文本视图的前一个值)转换为一个双精度值,而不是坐标。
感谢您发送编修。

相关问题