2013-10-06 114 views
0

我開發一個簡單的測驗應用程序,我有一個問題,我認爲這些代碼。當我單擊提交按鈕時,如果文本字段留空,它會崩潰。這是我的代碼。的Android應用程序測驗崩潰

public class Quiz extends Activity 
{ 

Button submit; 
EditText e1,e2,e3,e4,e5; 
int ctr; 
String msg = "", msg1 = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 
    submit=(Button)findViewById(R.id.button1); 
    e1=(EditText)findViewById(R.id.editText1); 
    e2=(EditText)findViewById(R.id.editText2); 
    e3=(EditText)findViewById(R.id.editText3); 
    e4=(EditText)findViewById(R.id.editText4); 
    e5=(EditText)findViewById(R.id.editText5); 
    submit.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0){ 
       String ans1 = String.valueOf(e1.getText()); 
       int ans11 = Integer.parseInt(ans1); 
       String ans2 = String.valueOf(e2.getText()); 
       int ans22 = Integer.parseInt(ans2); 
       String ans3 = String.valueOf(e3.getText()); 
       int ans33 = Integer.parseInt(ans3); 
       String ans4 = String.valueOf(e4.getText()); 
       int ans44 = Integer.parseInt(ans4); 
       String ans5 = String.valueOf(e5.getText()); 
       int ans55 = Integer.parseInt(ans5); 

       if(e1.getText().toString().equals("")||e2.getText().toString().equals("")||e3.getText().toString().equals("")||e4.getText().toString().equals("")||e5.getText().toString().equals("")){ 
        msg1 = "Text fields cannot be empty."; 
        Toast.makeText(Quiz.this, msg1, Toast.LENGTH_LONG).show(); 
        } 

       else{ 
       if(ans11==4){ 
        ctr++; 
       } 
       if(ans22==3){ 
        ctr++; 
       } 
       if(ans33==1){ 
        ctr++; 
       } 
       if(ans44==2){ 
        ctr++; 
       } 
       if(ans55==5){ 
        ctr++; 
       } 
       msg = "Your score is " + ctr; 
       Toast.makeText(Quiz.this, msg, Toast.LENGTH_LONG).show(); 
       } 


      } 
     }); 
} 

的logcat的說:

10-06 21:47:35.888: E/AndroidRuntime(1759): FATAL EXCEPTION: main 
10-06 21:47:35.888: E/AndroidRuntime(1759): java.lang.NumberFormatException: unable to parse '' as integer 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at java.lang.Integer.parseInt(Integer.java:362) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at java.lang.Integer.parseInt(Integer.java:332) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at com.example.capslock.Quiz$1.onClick(Quiz.java:39) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.view.View.performClick(View.java:2485) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.view.View$PerformClick.run(View.java:9080) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.os.Handler.handleCallback(Handler.java:587) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.os.Looper.loop(Looper.java:130) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at java.lang.reflect.Method.invoke(Method.java:507) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
10-06 21:47:35.888: E/AndroidRuntime(1759):  at dalvik.system.NativeStart.main(Native Method) 
+0

什麼說LogCoat? –

+0

是的LogCat說什麼? – Clark

回答

1

嘗試從EDITTEXT與

editText.getText().toString(); 

得到您的字符串,如果你想比較的另一個字符串字符串做這樣的:

if(ans1.equals("4")){...} 

如果你想從一個整數一個EditText上試試這個

int a11 = 0; 
try { 
    a11 = Integer.parseInt(editText.getText().toString()); 
}catch(NumberFormatException e){ } 

所以你沒有得到一個例外,當沒有字符串或在你的EditText一個litteral字符串

我是對的,你試圖從「」解析整數: -D

+1

只要有可能,應儘可能避免引發和捕獲異常,因爲它是如此昂貴的操作。在這裏說'String value = editText.getText()。toString(); if(editText!= null&editText.length> 0){a11 = Integer.parseInt(value);}' – Simon