Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.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
Java 如何从EditText字段中获取值并将其存储在双变量中?_Java_Android - Fatal编程技术网

Java 如何从EditText字段中获取值并将其存储在双变量中?

Java 如何从EditText字段中获取值并将其存储在双变量中?,java,android,Java,Android,以下是我的代码: public class MainActivity extends AppCompatActivity { Spinner dropdown; EditText e1; //user enters the bill amount before tip here TextView tipped; //total tipped amount TextView Bill; //the total amount of the bill including tax String sd

以下是我的代码:

public class MainActivity extends AppCompatActivity
{
Spinner dropdown;
EditText e1;  //user enters the bill amount before tip here
TextView tipped; //total tipped amount
TextView Bill;  //the total amount of the bill including tax
String sdropdown;
double originalBill = 0;  //amount that the user enters converted to a double
double result = 0;  //result of multiplying bill and tax
String test1;  //editText field has to be converted to a string value first
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();  //currency format object



//Create array of items
String tip [] = {"10%", "15%", "20%", "25%", "30%"};

//Create ArrayAdaptor
ArrayAdapter<String> adaptorTipAmount;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Create the element
    dropdown = (Spinner) findViewById(R.id.tipAmount);
    tipped = (TextView) findViewById(R.id.tipTotal);
    Bill = (TextView) findViewById(R.id.billTotal);
    e1 = (EditText) findViewById(R.id.billAmount);
    tipped.setText(currencyFormat.format(0));
    Bill.setText(currencyFormat.format(0));


    //initialize and set adaptor
    adaptorTipAmount = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, tip);

    adaptorTipAmount.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dropdown.setAdapter(adaptorTipAmount);



    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapter, View v,
                                   int position, long id) {

            // On selecting a spinner item
            sdropdown = adapter.getItemAtPosition(position).toString();

            //what editText field is initially stored in
            test1 = e1.getText().toString();

            //test1 string variable gets converted to a double
            //originalBill = Double.parseDouble(test1);



            //determines the tip amount and does the calculation based on the tip
           /* if (sdropdown.equals("10%"))
            {
                result = originalBill * .10;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("15%"))
            {
                result = originalBill * .15;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("20%"))
            {
                result = originalBill * .20;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("25%"))
            {
                result = originalBill * .25;
                tipped.setText(Double.toString(result));
            }
            else if (sdropdown.equals("30%"))
            {
                result = originalBill * .30;
                tipped.setText(Double.toString(result));
            }
            */
            tipped.setText("The tipped amount is: " + test1);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub
        }
    });
}
}
public类MainActivity扩展了AppCompatActivity
{
旋转器下拉列表;
EditText e1;//用户在此输入提示前的账单金额
TextView小费;//总小费金额
TextView Bill;//账单的总金额(含税)
字符串下载;
double originalBill=0;//用户输入的金额转换为double
double result=0;//票据与税金相乘的结果
String test1;//必须首先将editText字段转换为字符串值
NumberFormat currencyFormat=NumberFormat.getCurrencyInstance();//货币格式对象
//创建项目数组
字符串提示[]={“10%”、“15%”、“20%”、“25%”、“30%”;
//创建阵列适配器
阵列适配器安装;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建元素
下拉菜单=(微调器)findViewById(R.id.tipAmount);
tipped=(TextView)findViewById(R.id.tipTotal);
Bill=(TextView)findViewById(R.id.billTotal);
e1=(EditText)findViewById(R.id.billAmount);
setText(currencyFormat.format(0));
Bill.setText(currencyFormat.format(0));
//初始化并设置适配器
AdapterTipaMount=新阵列适配器(此,
android.R.layout.simple\u微调器\u项目,提示);
AdapterTipaMount.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
下拉设置适配器(适配器安装);
setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(适配器视图适配器,视图v,
内部位置,长id){
//关于选择微调器项
sdropdown=adapter.getItemAtPosition(位置).toString();
//最初存储在哪个editText字段中
test1=e1.getText().toString();
//test1字符串变量被转换为双精度
//originalBill=Double.parseDouble(test1);
//确定小费金额并基于小费进行计算
/*if(sdropdown.equals(“10%”)
{
结果=originalBill*.10;
tipped.setText(Double.toString(result));
}
else if(sdropdown.equals(“15%”)
{
结果=originalBill*.15;
tipped.setText(Double.toString(result));
}
else if(sdropdown.equals(“20%”)
{
结果=originalBill*.20;
tipped.setText(Double.toString(result));
}
else if(sdropdown.equals(“25%”)
{
结果=originalBill*.25;
tipped.setText(Double.toString(result));
}
else if(sdropdown.equals(“30%”)
{
结果=originalBill*.30;
tipped.setText(Double.toString(result));
}
*/
tipped.setText(“倾翻量为:“+test1”);
}
@凌驾
未选择公共无效(AdapterView arg0)
{
//TODO自动生成的方法存根
}
});
}
}
我遇到的问题是,每当我试图从EditText字段中获取值并将其存储在一个双精度文件中时,应用程序就会崩溃。我到处寻找这个问题的解决方案,发现如果先将字段存储在字符串中,然后将其转换为双精度,那么它就不起作用了。我很确定这是我的错误,尽管我没有办法解决这个问题。我也不想用按钮。有什么帮助吗?谢谢

试试这个:

在下面的结尾使用toString()如何:

tipped.setText(currencyFormat.format(0)).toString();

希望能有所帮助。

首先,您需要了解这两种类型之间的区别
double
是一种基本类型,而
double
是一种对象。在全局变量中:
Double originalBill

//what editText field is initially stored in
test1 = String.valueOf(e1.getText().toString());  //clear any chance if no string in editText

//test1 string variable gets converted to a double
if(test1 != null)
originalBill = Double.parseDouble(test1);

您可能需要清理从EditText获得的字符串。使用trim()删除任何空白(如果有)

        //what editText field is initially stored in
        test1 = e1.getText().toString().trim();
看看字符串中存储了什么值

        Log.v("parseDouble", "test1 = " + test1);
如果为空,请确保它不是空的,并将其设为“0”


你能再具体一点吗?它到底在哪里崩溃,异常是什么(如果有的话)。一般来说,我可以看到您在没有验证结果的情况下执行了很多findViewBy Id。这是自找麻烦。将代码包装到try/catch块中并检查异常也有很大帮助。它在originalBill=parsedouble处崩溃……如果我将其注释掉,然后尝试输出test1,这是一个字符串,它会工作,但尝试输出originalBill不会工作。您会得到什么异常?如果获得NumberFormatException,则字符串的格式不正确。顺便说一句,您试图解析的字符串中有什么?如果您得到NullPointerException-您没有成功地从表单中检索字符串。我使用test1变量并获取EditText字段e1的值。为了测试这一点,我做了一个setText并输出test1的值。然后,我尝试将test1解析为double并将其存储在originalBill变量中,但失败了。我通常会将其注释掉,但在上面的代码中没有注释。setText()返回void。不能对其调用toString()。它的目的是什么?
        if (test1.isEmpty()) test1 = "0";
        //test1 string variable gets converted to a double
        originalBill = Double.parseDouble(test1);