我正在构建John Horton撰写的“通过构建Android游戏学习Java”第2章和第3章中的数学游戏。我已接近第3章的末尾,并且已准确编写了本书的建议,但我的游戏在我的AVD中不断崩溃。您不必阅读本书就知道我在说什么我不相信,而且我不是在寻找有关如何改进我的代码的建议,因为本书首先教授最基本的方面,然后深入研究更复杂和简洁的方法。基本上,游戏的目标是创建一个向用户呈现数学问题的应用程序,并为用户提供关于问题答案的三个选项。
这是我的MainActivity.java:
package snzy.mathgamechapter2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
@Override
public void onClick (View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
这是我的GameActivity.java:
package snzy.mathgamechapter2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class GameActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
int partA = 9;
int partB = 9;
int correctAnswer = partA*partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;
TextView textObjectPartA =
(TextView)findViewById(R.id.textPartA);
TextView textObjectPartB =
(TextView)findViewById(R.id.textPartB);
Button buttonObjectChoice1 =
(Button)findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 =
(Button) findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 =
(Button) findViewById(R.id.buttonChoice3);
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partB);
buttonObjectChoice1.setText("" +correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
}
}
这是我的logcat:
> 06-06 15:46:41.038 6951-6951/? I/art: Not late-enabling -Xcheck:jni
> (already on) 06-06 15:46:41.139 6951-6951/snzy.mathgamechapter2
> W/System: ClassLoader referenced unknown path:
> /data/app/snzy.mathgamechapter2-2/lib/x86 06-06 15:46:41.245
> 6951-6951/snzy.mathgamechapter2 D/AndroidRuntime: Shutting down VM
> 06-06 15:46:41.276 6951-6951/snzy.mathgamechapter2 E/AndroidRuntime:
> FATAL EXCEPTION: main
> Process: snzy.mathgamechapter2, PID: 6951
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{snzy.mathgamechapter2/snzy.mathgamechapter2.MainActivity}:
> java.lang.ClassCastException: android.widget.RelativeLayout cannot be
> cast to android.widget.Button
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
> Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
> cannot be cast to android.widget.Button
> at snzy.mathgamechapter2.MainActivity.onCreate(MainActivity.java:15)
> at android.app.Activity.performCreate(Activity.java:6237)
> at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-06
> 15:46:55.432 6951-6951/? I/Process: Sending signal. PID: 6951 SIG: 9
非常感谢任何帮助。
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="snzy.mathgamechapter2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/buttonPlay" />
</android.support.design.widget.CoordinatorLayout>
这是我的content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="baylamafia.mathgamechapter2.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math game"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_marginTop="17dp"
android:layout_alignEnd="@+id/button2"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_alignStart="@+id/button2" />
</RelativeLayout>
答案 0 :(得分:0)
你应该发布你的layout/activity_main.xml
布局文件,但我猜这行:
Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
崩溃,因为布局中的R.id.buttonPlay
实际上是RelativeLayout,而不是Button。