我是Android开发的新手,所以如果这个问题看似微不足道,请原谅我。
我正在尝试在我的手机上运行我的Android应用程序(HTC One),但它在我使用键盘填充文本区域时停止工作。我知道eclipse提供的模拟器的修复,但有人可以请我在真实设备上使用它吗?
谢谢!
更新: 请通过以下LogCat查找错误日志:
E/AndroidRuntime(24443): FATAL EXCEPTION: main
E/AndroidRuntime(24443): java.lang.NumberFormatException: Invalid double: "android.widget.EditText@421edb70"
E/AndroidRuntime(24443): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
E/AndroidRuntime(24443): at java.lang.StringToReal.initialParse(StringToReal.java:114)
E/AndroidRuntime(24443): at java.lang.StringToReal.parseDouble(StringToReal.java:263)
E/AndroidRuntime(24443): at java.lang.Double.parseDouble(Double.java:295)
E/AndroidRuntime(24443): at com.example.crazytipcalc.CrazyTipCalc.updateTipAndFinalBill(CrazyTipCalc.java:81)
E/AndroidRuntime(24443): at com.example.crazytipcalc.CrazyTipCalc.access$1(CrazyTipCalc.java:79)
E/AndroidRuntime(24443): at com.example.crazytipcalc.CrazyTipCalc$1.onTextChanged(CrazyTipCalc.java:73)
E/AndroidRuntime(24443): at android.widget.TextView.sendOnTextChanged(TextView.java:7452)
E/AndroidRuntime(24443): at android.widget.TextView.handleTextChanged(TextView.java:7511)
E/AndroidRuntime(24443): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9317)
E/AndroidRuntime(24443): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
E/AndroidRuntime(24443): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
E/AndroidRuntime(24443): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
E/AndroidRuntime(24443): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
E/AndroidRuntime(24443): at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
E/AndroidRuntime(24443): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
E/AndroidRuntime(24443): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
E/AndroidRuntime(24443): at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:189)
E/AndroidRuntime(24443): at android.widget.TextView.doKeyDown(TextView.java:5432)
E/AndroidRuntime(24443): at android.widget.TextView.onKeyDown(TextView.java:5250)
E/AndroidRuntime(24443): at android.view.KeyEvent.dispatch(KeyEvent.java:2726)
E/AndroidRuntime(24443): at android.view.View.dispatchKeyEvent(View.java:7142)
E/AndroidRuntime(24443): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1369)
E/AndroidRuntime(24443): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1369)
E/AndroidRuntime(24443): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1369)
E/AndroidRuntime(24443): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1369)
E/AndroidRuntime(24443): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2013)
E/AndroidRuntime(24443): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1490)
E/AndroidRuntime(24443): at android.app.Activity.dispatchKeyEvent(Activity.java:2414)
E/AndroidRuntime(24443): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1940)
E/AndroidRuntime(24443): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4048)
E/AndroidRuntime(24443): at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:4004)
E/AndroidRuntime(24443): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3530)
E/AndroidRuntime(24443): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4648)
E/AndroidRuntime(24443): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4627)
E/AndroidRuntime(24443): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3347)
E/AndroidRuntime(24443): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24443): at android.os.Looper.loop(Looper.java:155)
E/AndroidRuntime(24443): at android.app.ActivityThread.main(ActivityThread.java:5536)
E/AndroidRuntime(24443): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24443): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(24443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
E/AndroidRuntime(24443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
E/AndroidRuntime(24443): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
package com.example.crazytipcalc;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
public class CrazyTipCalc extends Activity {
private static final String TOTAL_BILL = "TOTAL_BILL";
private static final String CURRENT_TIP = "CURRENT_TIP";
private static final String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";
private double billBeforeTip;
private double tipAmount;
private double finalBill;
EditText billBeforeTipET;
EditText tipAmountET;
EditText finalBillET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crazy_tip_calc);
if(savedInstanceState == null){
billBeforeTip = 0.0;
tipAmount = .15;
finalBill = 0.0;
} else{
billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
finalBill = savedInstanceState.getDouble(TOTAL_BILL);
}
billBeforeTipET = (EditText) findViewById(R.id.billEditText);
tipAmountET = (EditText) findViewById(R.id.tipEditText) ;
finalBillET = (EditText) findViewById(R.id.finalBillEditText);
billBeforeTipET.addTextChangedListener(billBeforeTipListener);
}
private TextWatcher billBeforeTipListener = new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
try{
billBeforeTip = Double.parseDouble(s.toString());
} catch(NumberFormatException e){
billBeforeTip = 0.0;
}
updateTipAndFinalBill();
}
};
private void updateTipAndFinalBill(){
double tipAmount = Double.parseDouble(tipAmountET.toString());
double finalBill = billBeforeTip + (billBeforeTip*tipAmount);
finalBillET.setText(String.format("%.02f", finalBill));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(TOTAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.crazy_tip_calc, menu);
return true;
}
}
答案 0 :(得分:1)
问题在于:Double.parseDouble(tipAmountET.toString());
您需要将其替换为:Double.parseDouble(tipAmountET.getText().toString());
private void updateTipAndFinalBill() {
try {
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
double finalBill = billBeforeTip + (billBeforeTip*tipAmount);
finalBillET.setText(String.format("%.02f", finalBill));
} catch(NumberFormatException e){
/* Whatever you want to do when there is no tip amount. */
}
}
答案 1 :(得分:1)
问题出在这里
double tipAmount = Double.parseDouble(tipAmountET.toString());
这里你只是想将EditText转换为双格式的String。所以它无法获得任何价值,因此你就得到了错误。所以改变它
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
答案 2 :(得分:1)
在updateTipAndFinalBill()
方法中,您要调用以下内容:
double tipAmount = Double.parseDouble(tipAmountET.toString());
首先,tipAmountET
是EditText
,因此该行应为:
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
其次,您应该使用try-catch
块来围绕它,就像您在onTextChanged()
的{{1}}方法中所做的那样。
TextWatcher