我是 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
你好,我是安卓编程新手。我试图在按下按钮时弹出一个带有文本输入的对话框。当我单击按钮时,应用程序崩溃。请问有什么帮助吗?