【发布时间】:2017-01-21 20:57:52
【问题描述】:
我是 Android 开发的新手,我正在尝试为学校项目构建一个应用程序。我在互联网上搜索并找到了一些我想要创建的数学应用程序的指南/代码。我复制/粘贴了其中的一些,并设法创建了更好的 UI。当我启动游戏时一切都很好,但是当我玩游戏时崩溃了。游戏是关于基本的数学运算。答案是随机的(例如 5x5=?),用户必须回答。如果答案是正确的,则会出现一个勾号,如果答案是错误的,则会出现一个叉号。当我使用该应用程序时,我设法输入了答案,但是当我单击 Enter 或 C 按钮时,应用程序崩溃了..有什么想法吗? (我是新手,我希望我能理解你对我说的话:P)谢谢!
主要的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)
【问题讨论】:
-
请发布您的日志。你可以在android studio的底部找到它,名称为
Android Monitor。在该选项中选择错误选项(默认为详细)。然后运行你的应用,当它崩溃时你会得到一个报告 -
发布崩溃表单logcat的堆栈跟踪。
-
感谢您的快速解答!添加了错误日志@NirmalRaj
-
希望你的意思和上面用户的意思一样!添加了错误日志! @GabeSechan
-
你能指出第 156 行是什么吗?只需点击它就会带你到那里