Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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/9/google-cloud-platform/3.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_Android Edittext - Fatal编程技术网

Java 将EditText中的字符串转换为双精度

Java 将EditText中的字符串转换为双精度,java,android,android-edittext,Java,Android,Android Edittext,我想制作一个小应用程序。 这个应用程序要做的是有一个运行的“银行账户”总数,有两个选项:存款或取款。 我附加了主活动类以及我为“银行账户”制作的类(我还没有实现历史记录功能,因为我还没有弄清楚这部分!)。 基本上,这一行: bankAccount.withdrawal(Double.parseDouble(findViewById(R.id.inputWithdrawal).toString())) 它的存款对应方抛出了一个NumberFormatException表示它是一个“无效的双精度”

我想制作一个小应用程序。
这个应用程序要做的是有一个运行的“银行账户”总数,有两个选项:存款或取款。
我附加了主活动类以及我为“银行账户”制作的类(我还没有实现历史记录功能,因为我还没有弄清楚这部分!)。
基本上,这一行:

bankAccount.withdrawal(Double.parseDouble(findViewById(R.id.inputWithdrawal).toString()))
它的存款对应方抛出了一个
NumberFormatException
表示它是一个“无效的双精度”。
我不知道我在其他线程上看到了什么,但我没能找到任何有效的

import android.support.v7.app.AppCompatActivity;   
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private EditText withdrawal, deposit;
    private Button withdrawalButton, depositButton;
    private BankAccount bankAccount;
    private String total;
    private TextView textViewTotal;

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

        withdrawal = (EditText) findViewById(R.id.inputWithdrawal);
        deposit = (EditText) findViewById(R.id.inputDeposit);

        withdrawalButton = (Button) findViewById(R.id.withdrawalButton);
        depositButton = (Button) findViewById(R.id.depositButton);
        withdrawalButton.setOnClickListener(onClickListener);
        depositButton.setOnClickListener(onClickListener);

        bankAccount = new BankAccount();
        textViewTotal = (TextView) findViewById(R.id.Total);
        updateTotal();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view){
            try {
                switch (view.getId()) {
                    case R.id.withdrawalButton:
                        if (!withdrawal.getText().toString().equals("")) {
                            bankAccount.withdrawal(Double.parseDouble(findViewById(R.id.inputWithdrawal).toString()));
                            updateTotal();
                        }
                        break;

                    case R.id.depositButton:
                        if (!deposit.getText().toString().equals("")) {
                            bankAccount.deposit(Double.parseDouble(findViewById(R.id.inputDeposit).toString()));
                            updateTotal();
                        }
                        break;
                }
            } catch (NumberFormatException e){
                e.printStackTrace();
            }
        }};

    public void updateTotal(){
        total = "$" + bankAccount.getCheckingTotal();
        textViewTotal.setText(total);
    }  
}

公共类银行账户{
私人复核合计;
私家侦探史;
公共银行账户(){
checkingTotal=0;
历史=新的ArrayList();
}
公开作废取款(双倍金额){
支票总额-=金额;
历史记录。添加(“-$”+金额);
如果(history.size()>5)
历史。删除(0);
}
公共作废保证金(双倍金额){
支票总额+=金额;
历史记录。添加(“$”+金额);
如果(history.size()>5)
历史。删除(0);
}
public double getCheckingTotal(){
返回检查总数;
}
}
findViewById(R.id.InputRetraction)
返回一个
视图

View.toString()
提供一些垃圾值(而不是
EditText
的内容)

您已经有
drawing=(EditText)findViewById(R.id.InputDrawing)
,因此使用它来获取字符串(就像您已经使用
getText().toString()
来查看字符串是否为空一样)

提示:始终避免额外的
findViewById
调用

findViewById(R.id.InputRetraction)
返回一个
视图

View.toString()
提供一些垃圾值(而不是
EditText
的内容)

您已经有
drawing=(EditText)findViewById(R.id.InputDrawing)
,因此使用它来获取字符串(就像您已经使用
getText().toString()
来查看字符串是否为空一样)


提示:始终避免额外的
findViewById
调用

读取错误消息。读取错误消息。
public class BankAccount {
    private double checkingTotal;
    private ArrayList<String> history;

    public BankAccount(){
        checkingTotal = 0;
        history = new ArrayList<String>();
    }

    public void withdrawal(double amount){
        checkingTotal -= amount;
        history.add("-$" + amount);

        if(history.size() > 5)
            history.remove(0);
    }

    public void deposit(double amount){
        checkingTotal += amount;
        history.add("$" + amount);

        if(history.size() > 5)
            history.remove(0);
    }

    public double getCheckingTotal(){
        return checkingTotal;
    }
}
String w = withdrawal.getText().toString();
if (!TextUtils.isEmpty(w)) {
    bankAccount.withdrawal(Double.parseDouble(w));
    updateTotal();
}