android - AVD中的简单数学游戏崩溃

标签 android crash

我正在根据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);

    }
}

这是我的日志:
> 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>

最佳答案

您应该发布layout/activity_main.xml布局文件,但是我猜这行:

Button buttonPlay = (Button) findViewById(R.id.buttonPlay);

之所以崩溃,是因为布局中的R.id.buttonPlay实际上是RelativeLayout,而不是Button。

关于android - AVD中的简单数学游戏崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668102/

相关文章:

android - Flutter - 选择 TextFormField 时键盘不显示

java - 使用 TableLayout,按钮超出边缘

javascript - Android 浏览器仍然使用在 Javascript 中创建事件的旧方法

assembly - Windows 7上的FS注册访问冲突

java - 由于 NullPointerException : Attempt to invoke virtual method HashMap on a null object reference 无法启动 Activity

android - Ionic android 构建失败,找不到插件 "proposal-numeric-separator"

android - 在哪里可以找到 Android 中的联系人 View ?

xcode - 运行项目时Xcode5.0.2崩溃

android - React Native应用程序在Android上启动时崩溃:找不到类 “com.facebook.react.devsupport.DevSupportManagerImpl”

iphone - 如何安全地停止 AVAudioPlayer