2013-05-12 37 views
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 

您好,即時通訊新到Android編程。我試圖在按下按鈕時使用文本輸入進行對話框彈出。當我點擊按鈕時,應用程序崩潰。請幫忙嗎?

+0

後logcat的錯誤 – Daniel 2013-05-12 02:09:58

+0

貼!有任何想法嗎? – 2013-05-12 03:04:45

+0

請檢查這個鏈接。這可能會有所幫助.. http://stackoverflow.com/questions/3968170/android-prompt-users-input-using-a-dialog – Porwal 2013-05-12 03:47:52

回答

1

在使用parseInt函數之前,您不檢查提供的變量是否爲空。

你應該修改代碼來防止這種情況,你得到的錯誤只是解釋了在這種情況下值是空的,所以函數不能從中返回一個整數。

您應該調試並檢查這3個變量的值:reduceLP,yourLP.getText().toString()opLP.getText().toString()

+0

+1好答案。但是OP應該注意不僅要檢查空值,還要確保它是一個'int',以防用戶放入類似於字母,空格,特殊字符等的東西。 – codeMagic 2013-05-12 04:20:15

0

從它的目錄下載清楚,你是解析非整數值,所以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()) 
} 
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)); 
} 

只要檢查yourLPoppLPreduceLP是否包含數字。

0

你或許應該做這兩件事情給simplyfy你的代碼,避免對空字符串的例外

  1. parseInt函數導致異常使檢查空虛的男人在上面的回答說。

  2. 始終使用LayoutInflator創建對話的自定義視圖,以便

AlertDialog.Builder SAVE_DIALOG =新AlertDialog.Builder(本);

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(); 

希望它可以幫助你