我的Java程序是:
package slidingmenu.androidhive.info.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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);
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;
}
}
我的logcat是:
04-12 19:27:24.372 1203- 1203/slidingmenu.androidhive.info.myapplication
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid double: "android.widget.EditText{5320176c VFED..CL ........ 32,257-736,336 #7f0c006d app:id/bill_amt}"
如何解决此错误。提前谢谢。
答案 0 :(得分:0)
猜测,但您可能无法在正确的对象上实现正确的方法。也许因为,amt变量的类型为TextEdit,请尝试将其更改为:
Double.parseDouble(amt.getText().toString());
Double.parseDouble(tip.getText().toString());
如果有帮助,请告诉我。