Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将编辑文本值转换为双精度-始终为零(android studio,java)_Java_Android_Double_Converter - Fatal编程技术网

将编辑文本值转换为双精度-始终为零(android studio,java)

将编辑文本值转换为双精度-始终为零(android studio,java),java,android,double,converter,Java,Android,Double,Converter,我正在制作一个android应用程序——当我设法运行时,出现了很多错误。我必须得到编辑文本的值并将其转换为双精度,起初它根本不起作用(应用程序因此崩溃),然后我设法让它运行,但现在它始终为零 例如,每次调用方法c2f时,resukt为32 **Main activity:** input = (EditText) findViewById(R.id.input); convert = (Button) findViewById(R.id.convert); result = (

我正在制作一个android应用程序——当我设法运行时,出现了很多错误。我必须得到编辑文本的值并将其转换为双精度,起初它根本不起作用(应用程序因此崩溃),然后我设法让它运行,但现在它始终为零

例如,每次调用方法c2f时,resukt为32

**Main activity:**

input = (EditText) findViewById(R.id.input);
    convert = (Button) findViewById(R.id.convert);
    result = (TextView) findViewById(R.id.result);
    c2f = (RadioButton) findViewById(R.id.c2f);
    c2k = (RadioButton) findViewById(R.id.c2k);
    f2c = (RadioButton) findViewById(R.id.f2c);
    f2k = (RadioButton) findViewById(R.id.f2k);
    k2c = (RadioButton) findViewById(R.id.k2c);
    k2f = (RadioButton) findViewById(R.id.k2f);

    double w;

    try {
        w = new Double(input.getText().toString());
    } catch (NumberFormatException e) {
        w = 0;
    }


    final double finalW = w;
    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            if (c2f.isChecked())
            {
                result.setText(Converter.c2f(finalW)+ "F");
            } else if (c2k.isChecked())
            {
                result.setText(Converter.c2k(finalW)+ "K");
            } else if (f2c.isChecked())
            {
                result.setText(Converter.f2c(finalW)+ "C");
            } else if (f2k.isChecked())
            {
                result.setText(Converter.f2k(finalW)+ "K");
            } else if (k2c.isChecked())
            {
                result.setText(Converter.k2c(finalW)+ "C");
            } else if (k2f.isChecked())
            {
                result.setText(Converter.k2f(finalW)+ "F");
            }

        }
    });


}}
类转换

public class Converter
{ 公共静态双c2f(双w){返回w*9/5+32;} 公共静态双c2k(双w) { 返回w+273.15; } 公共静态双f2c(双w) { 返回(w-32)*5/9; } 公共静态双f2k(双w){返回(w+459.67)*5/9;} 公共静态双k2c(双w) { 返回w-273.15; } 公共静态双k2f(双w) { 返回w*1.8-459.67; }
}

这是因为抛出异常,并且您已设置
w=0在catch块内。使用以下命令:

try {
    w = Double.parseDouble(input.getText().toString().trim());
} catch (NumberFormatException e) {
    e.printStackTrace();
    w = 0;
}
您还可以考虑将editText的inputType设置为number from xml:

<EditText
    android:id="@+id/edit_text"
    android:maxLines="1"
    android:inputType="numberDecimal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

这是因为抛出异常,并且您已设置
w=0在catch块内。使用以下命令:

try {
    w = Double.parseDouble(input.getText().toString().trim());
} catch (NumberFormatException e) {
    e.printStackTrace();
    w = 0;
}
您还可以考虑将editText的inputType设置为number from xml:

<EditText
    android:id="@+id/edit_text"
    android:maxLines="1"
    android:inputType="numberDecimal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

/**您只需使用以下代码即可**/
尝试
{
double value=double.valueOf(input.getText().toString());
}
捕获(NumberFormatException ex)
{
例如printStackTrace();
}
/**您只需使用以下代码即可**/
尝试
{
double value=double.valueOf(input.getText().toString());
}
捕获(NumberFormatException ex)
{
例如printStackTrace();
}

您好,谢谢您的回答。我在xml中指定了它。我应该把:w=Double.parseDouble(input.getText().toString().trim())放在哪里;嗨,谢谢你的回答。我在xml中指定了它。我应该把:w=Double.parseDouble(input.getText().toString().trim())放在哪里;