试图在Android Studio中开发掷骰子游戏,但不断崩溃

时间:2018-10-07 22:42:29

标签: java android android-studio main-activity android-studio-3.2

我正在尝试在Android Studio中构建Craps游戏。但是,当尝试运行该应用程序时,它会经历构建过程,然后在模拟器上启动该应用程序时,它崩溃并显示消息“不幸的是CrapsGame已停止”。我按照建议运行了logcat,它指示第12行有问题或ImageView有问题。但是,我对此并不陌生,所以我对问题所在感到十分困惑。

    @SuppressWarnings("PMD")
    @SpringBootApplication
    @EnableAutoConfiguration
    @EnableJpaRepositories(basePackages="com.myapp.repository")
    @ComponentScan("com.myapp")
    @EnableScheduling
    public class BatchMain {
package com.example.kkeosoukanh_mledna.crapsgamev2;

import android.support.annotation.Nullable;
import android.support.v4.app.BundleCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    //1. To get reference to the views
    ImageView dice1ImageView;
    ImageView dice2ImageView;
    Button rolldiceButton;
    Random randomNumberGenerator = new Random();
    int currentScore;
    int newScore;
    int updatedScore;


    TextView playerScore;
    TextView houseScore;
    private Object outState;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //2. Connecting the view to the layout so we can use it.
        dice1ImageView = findViewById(R.id.dice1_imageview);
        dice2ImageView = findViewById(R.id.dice2_imageview);
        rolldiceButton = findViewById(R.id.roll_button);
        playerScore = findViewById(R.id.playerScore_textview);
        houseScore = findViewById(R.id.houseScore_textview);

        // [button]
        rolldiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Simulate a roll from  1-6
                int roll1 = randomNumberGenerator.nextInt(6) + 1;
                int roll2 = randomNumberGenerator.nextInt(6) + 1;
                int sum = roll1 + roll2;

                String dieImage = "drawable/" + "die" + roll1;
                int imageKey = getResources().getIdentifier(dieImage, "drawable", getPackageName()); // actual id value
                dice1ImageView.setImageResource(imageKey);

                //For roll2
                //reusing imagekey and dieimage
                dieImage = "drawable/" + "die" + roll2;
                imageKey = getResources().getIdentifier(dieImage, "drawable", getPackageName());
                dice2ImageView.setImageResource(imageKey);


                /*
                . Each face contains one, two, three, four, five or six spots.
                 After the dice have come to rest, the sum of the spots on the two top faces is calculated.
                 If the sum is 7 or 11 on the first throw, the player wins.
                 If the sum is 2, 3 or 12 on the first throw (called “craps”), the player loses (the “house” wins).
                 */
                if (sum == 7 || sum == 11) {
                    Toast.makeText(MainActivity.this, "Player wins", Toast.LENGTH_LONG).show();
                    String currentScore = playerScore.getText().toString(); // "0"
                    int updatedScore = Integer.valueOf(currentScore) + 1; // 1

                    String newScore = String.valueOf(updatedScore); // "1"
                    playerScore.setText(newScore);


                } else if (sum == 2 || sum == 3 || sum == 12) {
                    Toast.makeText(MainActivity.this, "Craps. You lost. The house wins", Toast.LENGTH_LONG).show();
                    String currentScore = houseScore.getText().toString(); // "0"
                    int updatedScore = Integer.valueOf(currentScore) + 1; // 1

                    String newScore = String.valueOf(updatedScore); // "1"
                    houseScore.setText(newScore);
                }

                 /*
                If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes the player’s “point.”
                 To win, a player must continue rolling the dice until the point value is rolled.
                 The player loses by rolling a 7 before rolling the point.
                 */

                if (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10) {
                    int playerPoint = sum;
                    int newsum = 0;

                    while (newsum != 7 && newsum != playerPoint) {
                        int newroll1 = randomNumberGenerator.nextInt(6) + 1;
                        int newroll2 = randomNumberGenerator.nextInt(6) + 1;
                        newsum = newroll1 + newroll2;
                    }

                    if (newsum == 7) {
                        Toast.makeText(MainActivity.this, "House won", Toast.LENGTH_LONG).show();
                        String currentScore = houseScore.getText().toString(); // "0"
                        int updatedScore = Integer.valueOf(currentScore) + 1; // 1

                        String newScore = String.valueOf(updatedScore); // "1"
                        houseScore.setText(newScore);

                    } else {
                        Toast.makeText(MainActivity.this, "Player won", Toast.LENGTH_LONG).show();
                        String currentScore = playerScore.getText().toString(); // "0"
                        int updatedScore = Integer.valueOf(currentScore) + 1; // 1

                        String newScore = String.valueOf(updatedScore); // "1"
                        playerScore.setText(newScore);
                    }

                }


            }
        });


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("playerScore",currentScore);
        outState.putString("houseScore", String.valueOf(newScore));
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        currentScore = savedInstanceState.getInt("playerScore");
        newScore = savedInstanceState.getInt("houseScore");

    }



}
10-07 23:38:55.264 21853-21853/com.example.kkeosoukanh_mledna.crapsgamev2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kkeosoukanh_mledna.crapsgamev2, PID: 21853
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kkeosoukanh_mledna.crapsgamev2/com.example.kkeosoukanh_mledna.crapsgamev2.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class ImageView
        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: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class ImageView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.kkeosoukanh_mledna.crapsgamev2.MainActivity.onCreate(MainActivity.java:34)
        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) 
     Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class ImageView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.kkeosoukanh_mledna.crapsgamev2.MainActivity.onCreate(MainActivity.java:34) 
        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) 
     Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.kkeosoukanh_mledna.crapsgamev2:drawable/die1" (7f060055) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060055 a=-1 r=0x7f060055}
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
        at android.content.res.Resources.loadDrawable(Resources.java:2540)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.widget.ImageView.<init>(ImageView.java:152)
        at android.widget.ImageView.<init>(ImageView.java:140)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
        at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
        at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.kkeosoukanh_mledna.crapsgamev2.MainActivity.onCreate(MainActivity.java:34) 
        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) 

0 个答案:

没有答案