Android应用数学游戏崩溃

问题描述:

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

添加以下代码中的

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

对于所有10个按钮类似,并尝试运行您发布的相同代码。

+0

尝试了这个按钮0-10,但仍然是相同的行代码出错!也许我需要为输入按钮和清除按钮做类似的事情?因为当我按下其中一个时发生崩溃 –

您已将您的onClick回调函数绑定到所有按钮,包括enterclear按钮。但是,下面的代码行,这会导致问题

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

尝试获取输入保持在UI元素,并将其分配给enteredNum,这使得输入UI元素完美的意义,而不是对enterclear按钮。因此,你得到一个NullPointerException,因为没有价值可读。

我会提出以下方法。创建e onClick回调并将它们分配如下:

  • 一个在您的输入字段中。
  • 一个在您的“输入”按钮。
  • 一个在“清除”按钮上。

这将清楚地表明,当单击按钮enter时,您不希望读取任何内容。

用这种方法你可以分开每个按钮的业务逻辑。除了解决您的问题,这对于初级开发人员来说是一个很好的做法。

为了更容易阅读和维护,我还建议开始在单独的类中划分代码,作为一般原则。在第一章中涵盖这些原则的优秀阅读是Clean Code by Robert C. Martin

随时让我知道如果您的应用程序问题仍然存在。