如何修复“ java.lang.NumberFormatException:无效的double:”?

时间:2019-11-26 10:02:45

标签: android

  

我正在尝试从EditText获取文本,并将其作为双精度传递给此函数

     

这是我的代码:

 private MapViewLite mapView;
EditText lat,longs;
Button search;
String getLang,getlong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lat = findViewById(R.id.lat);
    longs = findViewById(R.id.longs);
    search = findViewById(R.id.search);
    // Get a MapViewLite instance from the layout.
    mapView = findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    getLang = lat.getText().toString();
    getlong = longs.getText().toString();
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadMapScene(getLang,getlong);
        }
    });
}
private void loadMapScene(String a,String b) {
    // Load a scene from the SDK to render the map with a map style.
    mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new LoadSceneCallback() {
        @Override
        public void onLoadScene(@Nullable SceneError sceneError) {
            if (sceneError == null) {
                try {
                    double aa = new Double(getLang);
                    double bb = new Double(getlong);

                    mapView.getCamera().setTarget(new GeoCoordinates(aa, bb));
                    mapView.getCamera().setZoomLevel(14);
                }catch (Exception e){
                    Log.e("Catch :", e.getMessage());
                     e.printStackTrace();
                }
            } else {
                Log.e("ERROR ->>> ", "onLoadScene failed: " + sceneError.toString());
            }
        }
    });
  

单击按钮后,我得到的错误为:

W/System.err: java.lang.NumberFormatException: Invalid double: ""
  

如何解决此错误,请回复

6 个答案:

答案 0 :(得分:1)

空字符串不被double接受,您可以执行以下操作

double dLang;

try {
    dLang= new Double(getLang);
} catch (NumberFormatException e) {
    dLang = 0; // your default value
}

答案 1 :(得分:1)

您正在呼叫

getLang = lat.getText().toString();
getlong = longs.getText().toString();

来自onCreate()

这些值未设置为yes,因此其文本为“”;

将它们移至loadMapScene()方法

答案 2 :(得分:0)

Add a check for value 
double aa,bb;
if(!TextUtils.isEmpty(getLang))
 aa = Double.parseDouble(getLang);
if(!TextUtils.isEmpty(getlong))
 bb = Double.parseDouble(getlong);

答案 3 :(得分:0)

像这样解析您的坐标以加倍

double aa = Double.parseDouble(getLang);
double bb = Double.parseDouble(getlong);

答案 4 :(得分:0)

我认为最佳做法是:

.cdk-overlay-container {
    z-index: 500 !important;
}

希望这会有所帮助。

答案 5 :(得分:0)

onCreate()内部执行此操作,以在用户单击搜索按钮时获得经度和纬度。

search.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        getLang = lat.getText().toString();
        getlong = longs.getText().toString();
        loadMapScene(getLang,getlong);
    }
});

loadMapScene()内部进行

private void loadMapScene(String a,String b) {
    double lat, lng;
    try {
        // Parse the string to double, this will throw exception if
        // parameter a or b contains any trace of string.
        lat = Double.parseDouble(a);
        lng = Double.parseDouble(b);
    } catch (NumberFormatException ex) {
        // We show a meaning message to user, so that they
        // enter valid location next time.
        toast("Location data invalid");
        // Here we get out of the method without executing any
        // other code in the current method
        return;
    }

    ...
    ...
}