无法读取微调器上的编辑文本输入

时间:2018-07-04 00:41:31

标签: android

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    myDialog = new Dialog(this);
    weight = (EditText) findViewById(R.id.editText);
    btn_result = (Button) findViewById(R.id.button);

    /*SPINER*/
    Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.names));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mySpinner.setAdapter(myAdapter);
    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            double x = Double.parseDouble(weight.getText().toString());
            switch (i) {
                case 1:
                    if (x <= 0.5) {
                        totalPrice = 5;
                        Price= totalPrice * 20 / 100;
                        Fee = totalPrice * 80 / 100;
                    } else {
                        totalPrice2 = x * 9;
                        Price= totalPrice * 20 / 100;
                        Fee = totalPrice * 80 / 100;
                    }
                    Toast.makeText(view.getContext(), "You selected case1", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    if (x <= 0.5) {
                        totalPrice2 = 7;
                        Price= totalPrice * 20 / 100;
                        Fee = totalPrice * 80 / 100;
                    } else {
                        totalPrice = x * 12.90;
                        Price= totalPrice * 20 / 100;
                        Fee = totalPrice * 80 / 100;
                    }
                    Toast.makeText(view.getContext(), "You selected case2", Toast.LENGTH_LONG).show();
                    break
                }
            }
        });
    }

    //here the problem cannot read the editText input 

    /*07-04 07:55:58.893 4268-4268/com.wedee.testalgo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wedee.testalgo, PID: 4268
java.lang.NumberFormatException: empty String
    at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
    at java.lang.Double.parseDouble(Double.java:547)
    at com.wedee.testalgo.MainActivity$1.onItemSelected(MainActivity.java:44)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:1319)
    at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1308)
    at android.widget.AdapterView.-wrap1(AdapterView.java)
    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1275)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)*/

edittext的输入值不能从edittext的输入值获取到微调器,可以任何建议如何获取它。... double x = Double.parseDouble(weight.getText()。toString());我从这条线中得到一个错误 我应该采取什么方法来获取输入信息?...

2 个答案:

答案 0 :(得分:1)

错误显示java.lang.NumberFormatException: **empty String**

在从微调框选择任何内容之前,您需要在double(权重)中输入一些EditText值。

答案 1 :(得分:0)

作为堆栈跟踪状态," "中有一个空字符串:EditText,无法转换为Double

解决此问题的两种方法可能是添加if语句以检查这种情况,或者是将属性添加到您的EditText XML:android:inputType,您可以将其配置为仅从EditText接收浮点数({ {1}}。

有关所有输入类型,请参见本Android文档的第一部分: https://developer.android.com/training/keyboard-input/style

相关问题