我是Android开发的新手,我正在尝试为学校项目构建应用程序。我搜索了互联网,找到了我想要创建的Math应用程序的一些指南/代码。我复制/粘贴其中一些,我设法创建了更好的用户界面。当我启动游戏时一切都很好,但是当我玩游戏时崩溃了。游戏是关于基本的数学运算。答案是随机的(例如5x5 =?),用户必须回答。如果答案是正确的,则会出现一个勾号,当出现错误时,会出现十字。当我使用该应用程序时,我设法输入答案,但当我点击进入或C按钮时,应用程序崩溃。任何想法? (我是新手,我希望我明白你对我说的话:P)谢谢!
Main 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)
答案 0 :(得分:0)
在代码中添加以下内容
btn1.setTag('1');
btn2.setTag('2');
btn3.setTag('3');
btn4.setTag('4');
同样适用于所有10个按钮,并尝试运行您发布的相同代码。
答案 1 :(得分:0)
您已将onClick回调功能绑定到所有按钮,包括enter
和clear
按钮。但是,以下代码行导致了问题
int enteredNum = Integer.parseInt(view.getTag().toString());
尝试获取UI元素中的输入并将其分配给enteredNum
,这在输入UI元素上非常有意义,但在enter
和clear
按钮上则没有。因此,您将获得NullPointerException
,因为没有值可供阅读。
我建议采用以下方法。创建e onClick回调并按如下方式分配它们:
这将清楚地表明,当点击enter
按钮时,您不想阅读任何内容。
通过这种方式,您可以分离每个按钮的业务逻辑。除了解决您的问题,对于初级开发人员来说,这将是一个很好的做法。
我还建议在不同的类中开始将代码分区,作为一般原则,以便更容易阅读和特别维护。在第一章中涵盖这些原则的优秀读物是Clean Code by Robert C. Martin。
如果您的申请问题仍然存在,请随时告诉我。