2017-01-16 69 views
0

我是Android開發新手,我正嘗試爲學校項目構建應用程序。我搜索了互聯網,並找到了一些我想要創建的數學應用的指南/代碼。我複製/粘貼了其中的一些,並設法創建了更好的用戶界面。當我啓動遊戲時一切都很好,但是當我玩遊戲時墜毀。這場比賽是關於基本的數學運算。答案是隨機的(例如,5x5 =?),用戶必須回答。如果答案是正確的,則會出現勾號,出現錯誤時會出現叉號。當我使用應用程序,我設法輸入答案,但當我點擊輸入或C按鈕應用程序崩潰..任何想法? (我是新手,希望我明白你對我說的話:P)謝謝!Android應用數學遊戲崩潰

主要XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_display_message" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

tools:context="com.example.miltos.maths.DisplayMessageActivity"> 

<include 
    android:id="@id/toolbar" 
    layout="@layout/tool_bar" 
    /> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#84b3e7"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/response" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:contentDescription="result" 
      android:src="@drawable/tick" 
      android:layout_gravity="bottom|left" /> 





    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 


     <TextView 
      android:id="@+id/question" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="2dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:background="#dfeaf1" 
      android:gravity="center|right" 
      android:padding="5dp" 
      android:text="0 + 0" 
      android:textColor="#ff333333" 
      android:textSize="30sp" /> 

     <TextView 
      android:id="@+id/answer" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="2dp" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:background="#dfeaf1" 
      android:gravity="center|left" 
      android:padding="5dp" 
      android:text="= ?" 
      android:textColor="#ff333333" 
      android:textSize="30sp" /> 

     <Button 
      android:id="@+id/clear" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#dfeaf1" 
      android:gravity="center" 
      android:padding="5dp" 
      android:text="C" 
      android:textColor="#ff333333" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 



    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn7" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="7" 
      android:text="7" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn8" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="8" 
      android:text="8" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn9" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="9" 
      android:text="9" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn4" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="4" 
      android:text="4" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn5" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="5" 
      android:text="5" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn6" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="6" 
      android:text="6" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="1" 
      android:text="1" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="2" 
      android:text="2" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn3" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="3" 
      android:text="3" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btn0" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:tag="0" 
      android:text="0" 
      android:textColor="#ffcccccc" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/enter" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_margin="1dp" 
      android:layout_weight="2" 
      android:background="#86959f" 
      android:gravity="center" 
      android:padding="5dp" 
      android:text="Enter" 
      android:textColor="#ff333333" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 





</LinearLayout> 

</RelativeLayout> 

Java代碼:

public class DisplayMessageActivity extends AppCompatActivity implements OnClickListener 

{ 
private int level = 0,answer = 0, operator = 0, operand1 = 0, operand2 = 0; 
private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3; 
private String[] operators = {"+", "-", "x", "/"}; 
private int[][] levelMax = { 
     {10, 25, 50}, 
     {10, 20, 30}, 
     {5, 10, 15}, 
     {10, 50, 100}}; 
private Random random; 
private TextView question, answerTxt; 
private ImageView response; 
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, enterBtn, clearBtn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 

    question = (TextView)findViewById(R.id.question); 
    answerTxt = (TextView)findViewById(R.id.answer); 
    response = (ImageView)findViewById(R.id.response); 
    response.setVisibility(View.INVISIBLE); 
    btn1 = (Button)findViewById(R.id.btn1); 
    btn2 = (Button)findViewById(R.id.btn2); 
    btn3 = (Button)findViewById(R.id.btn3); 
    btn4 = (Button)findViewById(R.id.btn4); 
    btn5 = (Button)findViewById(R.id.btn5); 
    btn6 = (Button)findViewById(R.id.btn6); 
    btn7 = (Button)findViewById(R.id.btn7); 
    btn8 = (Button)findViewById(R.id.btn8); 
    btn9 = (Button)findViewById(R.id.btn9); 
    btn0 = (Button)findViewById(R.id.btn0); 
    enterBtn = (Button)findViewById(R.id.enter); 
    clearBtn = (Button)findViewById(R.id.clear); 
    btn1.setOnClickListener(this); 
    btn2.setOnClickListener(this); 
    btn3.setOnClickListener(this); 
    btn4.setOnClickListener(this); 
    btn5.setOnClickListener(this); 
    btn6.setOnClickListener(this); 
    btn7.setOnClickListener(this); 
    btn8.setOnClickListener(this); 
    btn9.setOnClickListener(this); 
    btn0.setOnClickListener(this); 
    enterBtn.setOnClickListener(this); 
    clearBtn.setOnClickListener(this); 
    Bundle extras = getIntent().getExtras(); 
    if(extras != null) 
    { 
     int passedLevel = extras.getInt("level", -1); 
     if(passedLevel>=0) level = passedLevel; 
    } 

    random = new Random(); 
    chooseQuestion(); 
} 
private void chooseQuestion(){ 
    answerTxt.setText("= ?"); 
    operator = random.nextInt(operators.length); 
    operand1 = getOperand(); 
    operand2 = getOperand(); 

    if(operator == SUBTRACT_OPERATOR){ 
     while(operand2>operand1){ 
      operand1 = getOperand(); 
      operand2 = getOperand(); 
     } 
    } 

    else if(operator==DIVIDE_OPERATOR){ 
     while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2)) 
     { 
      operand1 = getOperand(); 
      operand2 = getOperand(); 
     } 
    } 
    switch(operator) 
    { 
     case ADD_OPERATOR: 
      answer = operand1+operand2; 
      break; 
     case SUBTRACT_OPERATOR: 
      answer = operand1-operand2; 
      break; 
     case MULTIPLY_OPERATOR: 
      answer = operand1*operand2; 
      break; 
     case DIVIDE_OPERATOR: 
      answer = operand1/operand2; 
      break; 
     default: 
      break; 
    } 
    question.setText(operand1+" "+operators[operator]+" "+operand2); 
} 
private int getOperand(){ 
    return random.nextInt(levelMax[operator][level] - levelMax[operator][level] + 1) 
      + levelMax[operator][level]; 

} 




@Override 
public void onClick(View view) { 

    if(view.getId()==R.id.enter){ 
     String answerContent = answerTxt.getText().toString(); 
     if(!answerContent.endsWith("?")) 
     { 
      int enteredAnswer = Integer.parseInt(answerContent.substring(2)); 
      if(enteredAnswer==answer){ 

       response.setImageResource(R.drawable.tick); 
       response.setVisibility(View.VISIBLE); 

      } 
      else{ 
       response.setImageResource(R.drawable.cross); 
       response.setVisibility(View.VISIBLE); 
      } 
      chooseQuestion(); 
     } 

    } 
    else if(view.getId()==R.id.clear){ 
     answerTxt.setText("= ?"); 
    } 
    else { 
     response.setVisibility(View.INVISIBLE); 
    } 

    int enteredNum = Integer.parseInt(view.getTag().toString()); 

    if(answerTxt.getText().toString().endsWith("?")) 
     answerTxt.setText("= "+enteredNum); 
    else 
     answerTxt.append(""+enteredNum); 
} 
} 

錯誤日誌:

01-16 16:04:35.300 4578-4614/com.example.miltos.maths E/EGL_emulation: tid 4614: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) 
01-16 16:04:37.016 4578-4614/com.example.miltos.maths E/EGL_emulation: tid  4614: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) 
01-16 16:04:39.157 4578-4578/com.example.miltos.maths E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.example.miltos.maths, PID: 4578 
                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
                     at com.example.miltos.maths.DisplayMessageActivity.onClick(DisplayMessageActivity.java:156) 
                     at android.view.View.performClick(View.java:5610) 
                     at android.view.View$PerformClick.run(View.java:22265) 
                     at android.os.Handler.handleCallback(Handler.java:751) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
+1

請張貼您的日誌。你可以在名爲'Android Monitor'的android studio的底部找到它。在那個選擇錯誤選項(Verbose將默認)。然後運行你的應用程序,當它崩潰時,你會得到一個報告 –

+0

張貼崩潰形式logcat的堆棧跟蹤。 –

+0

感謝您快速回答!錯誤日誌添加@NirmalRaj –

回答

0

添加以下代碼中的

btn1.setTag('1'); 
btn2.setTag('2'); 
btn3.setTag('3'); 
btn4.setTag('4'); 

對於所有10個按鈕類似,並嘗試運行您發佈的相同代碼。

+0

嘗試了這個按鈕0-10,但仍然是相同的行代碼出錯!也許我需要爲輸入按鈕和清除按鈕做類似的事情?因爲當我按下其中一個時發生崩潰 –

0

您已將您的onClick回調函數綁定到所有按鈕,包括enterclear按鈕。但是,下面的代碼行,這會導致問題

int enteredNum = Integer.parseInt(view.getTag().toString());

嘗試獲取輸入保持在UI元素,並將其分配給enteredNum,這使得輸入UI元素完美的意義,而不是對enterclear按鈕。因此,你得到一個NullPointerException,因爲沒有價值可讀。

我會提出以下方法。創建e onClick回調並將它們分配如下:

  • 一個在您的輸入字段中。
  • 一個在您的「輸入」按鈕。
  • 一個在「清除」按鈕上。

這將清楚地表明,當單擊按鈕enter時,您不希望讀取任何內容。

用這種方法你可以分開每個按鈕的業務邏輯。除了解決您的問題,這對於初級開發人員來說是一個很好的做法。

爲了更容易閱讀和維護,我還建議開始在單獨的類中劃分代碼,作爲一般原則。在第一章中涵蓋這些原則的優秀閱讀是Clean Code by Robert C. Martin

隨時讓我知道如果您的應用程序問題仍然存在。