我正在创建一个应用程序,它从edittext中获取数字值,对它们进行计算,如计算折扣,更改货币等,然后将其显示在另一个edittext上。
我正在将值从字符串转换为Double,然后通过将double转换为字符串再次在屏幕中显示它。我已经标记了计算和显示值的行。
我的问题是,edittext上没有显示任何内容,并且logcat中没有生成错误。
public class Calculator extends Activity implements View.OnClickListener {
EditText carat,list,price,total,discount,converter,discamt,caratrs;
Button submit;
Button one, two, three, four, five, six, seven, eight, nine, zero, clear,dot,calc;
double int_list=0,int_carat=0,int_discount=0,int_converter=0,int_price=0,int_total=0,int_discamt=0,int_pricers=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
carat=(EditText)findViewById(R.id.carat);
list=(EditText)findViewById(R.id.list);
price=(EditText)findViewById(R.id.price);
total=(EditText)findViewById(R.id.total);
discount=(EditText)findViewById(R.id.discount);
converter=(EditText)findViewById(R.id.converter);
discamt=(EditText)findViewById(R.id.discamt);
caratrs=(EditText)findViewById(R.id.caratrs);
submit=(Button)findViewById(R.id.submit);
calc=(Button)findViewById(R.id.calculate);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
zero = (Button) findViewById(R.id.zero);
clear=(Button)findViewById(R.id.clear);
dot=(Button)findViewById(R.id.dot);
price.setInputType(InputType.TYPE_NULL);
total.setInputType(InputType.TYPE_NULL);
carat.setInputType(InputType.TYPE_NULL);
list.setInputType(InputType.TYPE_NULL);
converter.setInputType(InputType.TYPE_NULL);
discount.setInputType(InputType.TYPE_NULL);
discamt.setInputType(InputType.TYPE_NULL);
caratrs.setInputType(InputType.TYPE_NULL);
try{
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
zero.setOnClickListener(this);
dot.setOnClickListener(this);
}
catch(Exception e){
}
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(carat.isFocused())
{
carat.setText("");
}
else if(price.isFocused())
{
price.setText("");
}
else if(total.isFocused())
{
total.setText("");
}
else if(list.isFocused())
{
list.setText("");
}
else if(discount.isFocused())
{
discount.setText("");
}
else if(converter.isFocused())
{
converter.setText("");
}
else if(discamt.isFocused())
{
discamt.setText("");
}
else if(caratrs.isFocused())
{
caratrs.setText("");
}
}
});
---------------------------This is where all the calculation takes place including displaying the value on the edittext-----------------------------
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int_list = Double.parseDouble(list.getText().toString());
int_carat = Double.parseDouble(carat.getText().toString());
int_discount = Double.parseDouble(discount.getText().toString());
int_converter = Double.parseDouble(converter.getText().toString());
int_discamt = Double.parseDouble(discamt.getText().toString());
int_discamt = (int_list * int_discount) / 100;
int_price = int_list - int_discamt;
int_pricers = int_price * int_converter;
int_total = int_price * int_converter * int_carat;
price.setText(Double.toString(int_price));
discamt.setText(Double.toString(int_discamt));
caratrs.setText(Double.toString(int_pricers));
total.setText(Double.toString(int_total));
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void onClick(View v) {
if(carat.isFocused())
{
carat.setText(carat.getText().toString() + ((Button) v).getText());
}
else if(list.isFocused())
{
list.setText(list.getText().toString() +((Button)v).getText());
}
else if(discount.isFocused())
{
discount.setText(discount.getText().toString() +((Button)v).getText());
}
else if(converter.isFocused())
{
converter.setText(converter.getText().toString() +((Button)v).getText());
}
}
}
这是我在logcat中得到的东西
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ java.lang.NumberFormatException: Invalid double: ""
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at java.lang.StringToReal.invalidReal(StringToReal.java:63)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at java.lang.StringToReal.parseDouble(StringToReal.java:248)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at java.lang.Double.parseDouble(Double.java:295)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at com.diamond.traders.Calculator$2.onClick(Calculator.java:176)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.view.View.performClick(View.java:4438)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 18:00:14.918 14299-14299/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
答案 0 :(得分:1)
使用
double doubleValue;
edittext.setText(""+doubleValue);
OR
edittext.setText(String.valueOf(doubleValue));
答案 1 :(得分:1)
您必须对所有EditText字段进行验证。
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String list = list.getText().toString().trim();
String carat = carat.getText().toString().trim();
String discount = discount.getText().toString().trim();
String converter = converter.getText().toString().trim();
String discamt = discamt.getText().toString().trim();
if(list.length() > 0 && carat.length() > 0 && discount.length() > 0
&& converter.length() > 0 && discamt.length() > 0 )
{
try {
int_list = Double.parseDouble(list.getText().toString());
int_carat = Double.parseDouble(carat.getText().toString());
int_discount = Double.parseDouble(discount.getText().toString());
int_converter = Double.parseDouble(converter.getText().toString());
int_discamt = Double.parseDouble(discamt.getText().toString());
int_discamt = (int_list * int_discount) / 100;
int_price = int_list - int_discamt;
int_pricers = int_price * int_converter;
int_total = int_price * int_converter * int_carat;
price.setText(Double.toString(int_price));
discamt.setText(Double.toString(int_discamt));
caratrs.setText(Double.toString(int_pricers));
total.setText(Double.toString(int_total));
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
}else
{
Toast.makeText(context, "Please enter all value.", Toast.LENGTH_SHORT).show();
}
}
});
答案 2 :(得分:1)
java.lang.NumberFormatException: Invalid double: ""
......
m.err﹕ at java.lang.Double.parseDouble(Double.java:295)
它只是说明您尝试解析为String
的{{1}}之一是double
。
""
确保您的int_list = Double.parseDouble(list.getText().toString());
int_carat = Double.parseDouble(carat.getText().toString());
int_discount = Double.parseDouble(discount.getText().toString());
int_converter = Double.parseDouble(converter.getText().toString());
int_discamt = Double.parseDouble(discamt.getText().toString());
有价值。
您可以使用EditText
转换为String.valueOf(total)
。
注意:
确保您的String
不是空的,使用EditText
(可能需要其他一些验证,这只是一个示例)以验证并围绕代码尝试当有可能存在异常时捕获。
您可能在错误的ed.getText().toString().equals("")
中放置了ID,但在计算过程中仍然为空,请检查。
答案 3 :(得分:1)
Double.toString()
,因为它不考虑区域设置。考虑使用String.format(locale, "%f", 123D);
例如,您可以将locale
替换为Locale.US。
答案 4 :(得分:0)
您似乎正在尝试将空字符串解析为double。 你应该检查:
if (edittext.getText().length > 0)