我有3个edittext,1个输入价格,1个百分比和最后一个两个结果。它工作得很好,但是当我从第2个edittext删除输入时,应用程序会崩溃。任何想法我在哪里做错了?或者更好的方法来实现这个。我希望它可以使用由用户填充和清空的edittext。
这是代码::
public class Mastercode extends Activity implements TextWatcher ,AdapterView.OnItemSelectedListener/*,View.OnClickListener*/{
int tumbal1,tumbal2,tumbal3,tumbal4;
EditText ethint1, etpersen2, textvDP1;
Button bkalkulasi1;
// private final String[] items ={"Zona 1","Zona 2","Zona 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simulasikredit);
ethint1 = (EditText) findViewById(R.id.ethint);
etpersen2 = (EditText) findViewById(R.id.etpersen);
textvDP1 = (EditText) findViewById(R.id.textvDP);
bkalkulasi1 = (Button)findViewById(R.id.bkalkulasi);
// NumberFormat format = NumberFormat.getCurrencyInstance();
//((EditText) findViewById(R.id.ethint)).setText(format.format(ethint1));
// ((EditText) findViewById(R.id.textvDP)).setText(format.format(textvDP1));
etpersen2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text1 = ethint1.getText().toString();
String text2 = etpersen2.getText().toString();
int input1 = Integer.valueOf(text1);
int input2 = Integer.valueOf(text2);
if (text1.length() != 0) {
int output = (input1 * input2) / 100;
textvDP1.setText(""+output /*+ ""*/);
}
else if(text2.length() == 0/*trim().equals("")*/){
String input3 = Integer.toString(input2);
textvDP1.setText(""+input3);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
logcat日志:
02-16 14:43:12.331: E/AndroidRuntime(609): FATAL EXCEPTION: main
02-16 14:43:12.331: E/AndroidRuntime(609): java.lang.NumberFormatException: unable to parse '' as integer
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parse(Integer.java:433)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parseInt(Integer.java:422)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parseInt(Integer.java:382)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.valueOf(Integer.java:682)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.m4nd1r1tun45f1n4nc3.abcdefg.Mastercode$1.onTextChanged(Mastercode.java:43)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView.handleTextChanged(TextView.java:6172)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:123)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 14:43:12.331: E/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method)
编辑代码:
etpersen2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text1 = ethint1.getText().toString();
String text2 = etpersen2.getText().toString();
int input1 = 0;
int input2 = 0;
if(text1.length()>0)
input1 = Integer.valueOf(text1);
if(text2.length()>0)
input2 = Integer.valueOf(text2);
if (text1.length() != 0) {
int output = (input1 * input2) / 100;
textvDP1.setText(""+output /*+ ""*/);
}
else if(text2.length() == 0/*trim().equals("")*/){
/*String input3 = Integer.toString(input2);*/
textvDP1.setText(""/*+input3*/);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
它现在正在工作,谢谢你们
答案 0 :(得分:1)
如果输入为空,则此行将抛出错误
int input2 = Integer.valueOf(text2);
而是改为,
if(! text2.equals("")){
int input2 = Integer.valueOf(text2);
}
答案 1 :(得分:1)
更改这两行
int input1 = Integer.valueOf(text1);
int input2 = Integer.valueOf(text2);
到
int input1=0;
int input2=0;
if(text1.length()>0)
input1 = Integer.valueOf(text1);
if(text2.length()>0)
input2 = Integer.valueOf(text2);
如果无法将字符串解析为整数,则ValueOf( - )将抛出 NumberFormatException 。
以下是文档http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28java.lang.String%29
希望这会对你有所帮助。