我的代码是:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText amt = (EditText) findViewById(R.id.bill_amt);
final EditText tip = (EditText) findViewById(R.id.bill_per);
final TextView result = (TextView) findViewById(R.id.res);
Button calc = (Button) findViewById(R.id.button1);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());
double tip_cal = (amount * tip_per) / 100;
result.setText("Result : " + Double.toString(tip_cal));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
请更正此编码中显示的错误 android studio也在Result.text上显示警告 关于使用占位符'
的资源字符串答案 0 :(得分:2)
您想将EditText
解析为Double
错误 - amt.toString()
和tip.toString()
。哪个不正确(根本不可能)。
替换它:
double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());
有了这个:
double amount = Double.parseDouble(amt.getText().toString());
double tip_per = Double.parseDouble(tip.getText().toString());