0

我是 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

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

4

4 回答 4

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包含数字。

于 2013-05-12T05:24:26.513 回答
1

在使用该parseInt函数之前,您不会检查提供的变量是否为空。

您应该修改代码以防止这种情况发生,您得到的错误只是说明在这种情况下该值为空,因此函数无法从中返回整数。

您应该调试并检查这 3 个变量的值reduceLPyourLP.getText().toString()opLP.getText().toString().

于 2013-05-12T03:23:03.863 回答
0

您可能应该做这两件事来简化您的代码并避免异常

  1. 空字符串上的 parseInt 导致异常,因此请按照上述答案中的一个人所说的那样检查是否为空。

  2. 始终使用 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();

希望对你有帮助

于 2013-05-12T11:51:41.717 回答
0

从 catlog 可以清楚地看出您正在解析非整数值,因此 Numberformat 异常

关于@codeMagic cooment,您可以通过以下方式检查

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())
}
于 2013-05-12T05:09:22.093 回答