2011-11-14 138 views
0

只要我使用方法Game(),我的應用程序崩潰,爲什麼? 另外我想知道是否有可能使我的代碼更短的空間方面需要&更好。奇怪的錯誤+如何使此代碼更有效率?

代碼:

package com.aleksei.etb; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 

public class ETBetaActivity extends Activity implements View.OnClickListener { 

    Button answer_1, 
    answer_2,answer_3, 
    answer_4,main; 

    TextView q_textview, 
    TextView tip; 

    private String aString[]; 

    private int i1 = 0; 
    private int correct = 0; 

    private boolean alive = false; 

    MediaPlayer button_click; 

    private String[] questions ={"Question 1" , "Question 2","Question 5"}; 
    private String[] answers_correct ={"Correct answer 1", "Correct answer 2","Correct answer 3","Correct answer 4","Correct answer 5"}; 

    List<String> question_list = new ArrayList<String>(); 
    List<String> answer_list_correct = new ArrayList<String>(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      getData(); 
    } 

    @Override 
    public void onClick(View view) { 

     button_click = MediaPlayer.create(this, R.raw.button_click); 
     button_click.start(); 
     switch(view.getId()){ 

      case R.id.button5: //main 
          if(!alive) 
          alive = true; 
          break; 
      case R.id.button1: //answer_1 
          if(alive == false) 
           return; 
          if(correct(1)) 
           correct++;   
          break; 
      case R.id.button2: //answer_2 
          if(alive == false) 
           return; 
          if(correct(2)) 
           correct++; 
          break; 
      case R.id.button3: //answer_3 
          if(alive == false) 
           return; 
          if(correct(3)) 
           correct++;   
          break; 
      case R.id.button4: //answer_3 
          if(alive == false) 
           return; 
          if(correct(4)) 
           correct++; 
          break; 
       default: 
          break;  
      } 
      Game();  
    } 

    private boolean correct(int button){ 

     try { 
      for (int i = 0; i < answers_correct.length; i++){ 
       if(button == 1 && aString[0] == answers_correct[i] 
      || button == 2 && aString[1] == answers_correct[i] 
      || button == 3 && aString[2] == answers_correct[i] 
      || button == 4 && aString[3] == answers_correct[i]) 
         return true; 
      } 
     } 
     catch(Exception ex){ 
      System.out.println(ex); 
     } 
     return false; 
    } 

    private void Game(){ 

     if(i1 > questions.length) //no more questions 
      return; 
     main.setText("Next"); 
      try { 
        String answer_list[][] = { 
           {answers_correct[i1], "Answer 1-2" , "Answer 1-3" , "Answer 1-4"}, 
           {answers_correct[i1], "Answer 2-2" , "Answer 2-3" , "Answer 2-4"}, 
           {answers_correct[i1], "Answer 3-2" , "Answer 3-3" , "Answer 3-4"}, 
           {answers_correct[i1], "Answer 4-2" , "Answer 4-3" , "Answer 4-4"}, 
           {answers_correct[i1], "Answer 5-2" , "Answer 5-3" , "Answer 5-4"}}; 

        Collections.shuffle(Arrays.asList(answer_list[i1])); 
        answer_1.setText(answer_list[i1][0]); 
        answer_2.setText(answer_list[i1][1]); 
        answer_3.setText(answer_list[i1][2]); 
        answer_4.setText(answer_list[i1][3]); 
        aString[0] = answer_list[i1][0]; 
        aString[1] = answer_list[i1][1]; 
        aString[2] = answer_list[i1][2]; 
        aString[3] = answer_list[i1][3]; 
        q_textview.setText(questions[i1]); 
      } 
      catch(Exception e){ 
      System.out.println(e); 
      } 
      tip.setText(correct); 

     /*questions = question_list.toArray(new String[question_list.size()]); 
     answers_correct = answer_list_correct.toArray(new     String[answer_list_correct.size()]); 

     question.setText(questions[i1]);   

     answer_list_correct.remove(questions[i1]); 
     question_list.remove(questions[i1]);*/ 
     i1++; 
    } 
    private void getData(){ 
     //Getting the data 
     main = (Button) findViewById(R.id.button5); 
     answer_1 = (Button) findViewById(R.id.button1); 
     answer_2 = (Button) findViewById(R.id.button2); 
     answer_3 = (Button) findViewById(R.id.button3); 
     answer_4 = (Button) findViewById(R.id.button4); 
     q_textview = (TextView) findViewById(R.id.question); 
     tip = (TextView) findViewById(R.id.answ1); 

     //Making the buttons, actually work 
     main.setOnClickListener(this); 
     answer_1.setOnClickListener(this); 
     answer_2.setOnClickListener(this); 
     answer_3.setOnClickListener(this); 
     answer_4.setOnClickListener(this); 

      //Resets the text 
      //Note to self: Replace with another ContectView 
     main.setText("Begin!"); 
     answer_4.setText(""); 
     answer_3.setText(""); 
     answer_2.setText(""); 
     answer_1.setText(""); 

    /* for(String x : questions) { 
      for(String y : answers_correct){ 

      answer_list_correct.add(y); 
      question_list.add(x); 

      Collections.shuffle(answer_list_correct); 
      Collections.shuffle(question_list); 

      } 
     } */ 
    } 
} 

最好的問候。

+1

代碼審查是嚴格的工作代碼。一旦你的代碼正常工作,就可以隨時將它帶回Code Review。但是,我建議您至少在恢復代碼之前正確地縮進代碼。 –

回答

2

Game()中,您在頂部檢查i1 > questions.length,但在底部附近嘗試q_textview.setText(questions[i1]);。如果i1 == questions.length,那會拋出ArrayIndexOutOfBoundsException(希望我正確記住名稱),使它i1 >= questions.length。此外,您使用i1作爲answers_correctanswer_list的索引,這不會超出粘貼的questionsanswers_correct的範圍,但可能是真實的(儘管不太可能,您不會有更多問題而不是答案,您會?)。

correct,你if聲明

if(button == 1 && aString[0] == answers_correct[i] 
    || button == 2 && aString[1] == answers_correct[i] 
    || button == 3 && aString[2] == answers_correct[i] 
    || button == 4 && aString[3] == answers_correct[i]) 
    return true; 

可以縮短爲

if (aString[button-1] == answers_correct[i]) return true; 

(除非按鈕可以有值< 1> 4,那麼你就需要檢查button >= 1 && button <= 4太) 。但是,使用==來比較字符串是危險的,幾乎肯定是錯誤的。 ==比較引用的相等性,所以string1 == string2只有在引用同一個String實例的情況下才爲true。如果看起來如此,所有的字符串都來自源代碼中的字符串文字,它將會有用,因爲那樣每個文字只有一個實例,但是如果外部的字符串可以進入遊戲,您必須必須使用equals來比較字符串。在==會(偶然地)工作的情況下也使用equals

+0

+0.5,向上取整;半點關閉確認使用==可以在這裏工作。從技術上講,絕對的,我想我可以看到一個理由,比如想要區分默認值和用.equals()內容但不同實例(例如設置爲默認值,但是從UI控件輸入的值)注意到它是明確設置的),但是。幾乎從來沒有對:) –

+0

@DaveNewton感謝您不使用循環到;甚至;)是的,使用==比較幾乎肯定是一個錯誤,但一個不會總是顯示。所以我想指出爲什麼它可能在這裏工作更好,所以這是可以理解的爲什麼它是錯的,即使它'工作'。 –

+0

謝謝,現在就試試。 – Alex