我是Android编程新手。当按下按钮时,我正在尝试使用文本输入创建一个对话框弹出窗口。当我单击按钮时,应用程序崩溃。有什么帮助吗?
enterLP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
TextView yourLP = (TextView) findViewById(R.id.textView4);
TextView oppLP = (TextView) findViewById(R.id.textView3);
int lifePoints;
if (youToggled.isChecked()){
lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
yourLP.setText(Integer.toString(lifePoints));
}
if (oppToggled.isChecked()){
lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
oppLP.setText(Integer.toString(lifePoints));
}
}
});
public void showDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Subtract Life Points:");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
reduceLP = input.getText().toString();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
LogCat错误:
05-12 01:59:30.002: D/dalvikvm(996): GC_FOR_ALLOC freed 54K, 8% free 2458K/2656K, paused 82ms, total 87ms
05-12 01:59:30.011: I/dalvikvm-heap(996): Grow heap (frag case) to 3.157MB for 650176-byte allocation
05-12 01:59:30.102: D/dalvikvm(996): GC_FOR_ALLOC freed 2K, 7% free 3090K/3292K, paused 82ms, total 83ms
05-12 01:59:30.212: D/dalvikvm(996): GC_CONCURRENT freed <1K, 7% free 3090K/3292K, paused 13ms+9ms, total 111ms
05-12 01:59:30.723: D/dalvikvm(996): GC_CONCURRENT freed 646K, 22% free 2849K/3640K, paused 18ms+23ms, total 182ms
05-12 01:59:31.272: D/gralloc_goldfish(996): Emulator without GPU emulation detected.
05-12 01:59:47.924: D/dalvikvm(996): GC_CONCURRENT freed 47K, 11% free 3251K/3640K, paused 75ms+108ms, total 305ms
05-12 01:59:48.021: D/AndroidRuntime(996): Shutting down VM
05-12 01:59:48.021: W/dalvikvm(996): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-12 01:59:48.062: E/AndroidRuntime(996): FATAL EXCEPTION: main
05-12 01:59:48.062: E/AndroidRuntime(996): java.lang.NumberFormatException: Invalid int: ""
05-12 01:59:48.062: E/AndroidRuntime(996): at java.lang.Integer.invalidInt(Integer.java:138)
05-12 01:59:48.062: E/AndroidRuntime(996): at java.lang.Integer.parseInt(Integer.java:359)
05-12 01:59:48.062: E/AndroidRuntime(996): at java.lang.Integer.parseInt(Integer.java:332)
05-12 01:59:48.062: E/AndroidRuntime(996): at com.aksynial.ygolpcalc.MainActivity$11.onClick(MainActivity.java:232)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.view.View.performClick(View.java:4204)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.view.View$PerformClick.run(View.java:17355)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.os.Handler.handleCallback(Handler.java:725)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.os.Looper.loop(Looper.java:137)
05-12 01:59:48.062: E/AndroidRuntime(996): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 01:59:48.062: E/AndroidRuntime(996): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 01:59:48.062: E/AndroidRuntime(996): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 01:59:48.062: E/AndroidRuntime(996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 01:59:48.062: E/AndroidRuntime(996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 01:59:48.062: E/AndroidRuntime(996): at dalvik.system.NativeStart.main(Native Method)
05-12 02:04:48.231: I/Process(996): Sending signal. PID: 996 SIG: 9
你好,我是android编程新手。当按下按钮时,我正在尝试使用文本输入创建一个对话框弹出窗口。当我单击按钮时,应用程序崩溃。有什么帮助吗?
答案 0 :(得分:1)
在使用parseInt
函数之前,不要检查提供的变量是否为空。
你应该修改代码来防止这种情况,你得到的错误只是解释了在这种情况下值是空的,所以函数不能从中返回一个整数。
您应该调试并检查这3个变量的值:reduceLP
,yourLP.getText().toString()
和opLP.getText().toString()
。
答案 1 :(得分:1)
这两行 -
lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
yourLP.setText(Integer.toString(lifePoints));
lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
oppLP.setText(Integer.toString(lifePoints));
更改为 -
if(yourLP.getText().toString().matches("\\d+") || reduceLP..matches("\\d+") )
{
lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
yourLP.setText(Integer.toString(lifePoints));
}
if(yourLP.getText().toString().matches("\\d+") || reduceLP..matches("\\d+"))
{
lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
yourLP.setText(Integer.toString(lifePoints));
}
只需检查yourLP
,oppLP
和reduceLP
是否包含数字。
答案 2 :(得分:0)
从catlog中可以清楚地看到你正在解析非整数值,所以Numberformat异常
关于@codeMagic冷却方面,您可以通过以下方式查看
public boolean isNumeric(String str) {
int ascii = 0;
boolean isNumeric = false;
for (int i = 0; i < str.length(); i++) {
ascii = (int) str.charAt(i);
if ((ascii >= 48 && ascii <= 57)) {
isNumeric = true;
if (isNumeric) {
return isNumeric;
}
}
}
return isNumeric;
}
现在,您可以查看
if(isNumeric(yourLP.getText().toString())){
lifePoints = Integer.parseInt(yourLP.getText().toString())
}
答案 3 :(得分:0)
您可能应该做这两件事来简化代码并避免异常
parseInt在空字符串上导致异常,所以检查空虚,就像上面的答案所说。
始终使用LayoutInflator为对话框创建自定义视图,以便
AlertDialog.Builder save_dialog = new AlertDialog.Builder(this);
save_dialog.setTitle(R.id.input_dialog_title);
LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
save_dialog.setTitle("Save your file");
View custom=inflator.inflate(R.layout.custom_input_dialog, null);
file_save_name=(EditText)custom.findViewById(R.id.input_file_name);
save_dialog.setView(custom);
save_dialog.setCancelable(true);
save_as_ringtone=save_dialog.create();
save_as_ringtone.show();
希望它可以帮到你