android studio 上的简单猜数字游戏应用程序。当 运行 我的物理设备中的 apk 立即强制关闭

Simple guess the number game app on android studio. When running the apk in my physical device it force closes instantly

我正在做一个简单的猜数字游戏。我希望它在开始时生成一个随机数。然后当我输入一个数字并确认它会检查它是否与生成的随机数匹配。你有 5 次机会,当你弄错时,它会从 5 中扣除一次尝试。当我按下重试时,我希望随机数和尝试计数重置。 XML 文件如下 [这是应用布局][1]

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:text="@string/the_guessing_game"
        android:textColor="#519169"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.474"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.074" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="372dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="@string/number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        android:autofillHints="" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/are_you_ready_guess_a_number_between_1_20_and_confirm"
        app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.888" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/confirm"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
        app:layout_constraintVertical_bias="0.259"
        android:onClick="onConfirmClick"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/retry"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.976"
        android:onClick="onRetryClick"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的主要 java 文件

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
    Random random = new Random();
    int number = random.nextInt(20);
    int tries = 5;

    TextView result = findViewById(R.id.textView);
    EditText confirm = findViewById(R.id.editTextTextPersonName);

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

    public void onConfirmClick (View view){
        int pass = Integer.parseInt(confirm.getText().toString());
        if (tries >= 0) {
            confirm.setText("Sorry your chances are over. Please retry");
        } else {
            if (pass != number) {
                tries = tries - 1;
                confirm.setText("That's kinda wrong. chances remaining: " + tries);

            } else if (pass == number) {
                confirm.setText("NICE. your answer is correct!. press retry to play again.");

            } else {
                confirm.setText("Don't you understand what a number is?");
            }

        }

        }

    public void onRetryClick (View view){
        number = random.nextInt(20);
        tries = 5;
        }
    }

我似乎找不到问题所在。一切似乎都很好,但 apk 强制关闭。在此先感谢您的帮助。


Edit: i checked all the ide given errors. I corrected those. Still its not running

  [1]: https://i.stack.imgur.com/5K6ZE.png

findViewById 应该在onCreate 里面,请尝试这样使用它!如果您仍然遇到崩溃,请转到下面显示的 运行 选项卡并从那里分享错误。 如果此解决方案对您有帮助,请标记为正确。 谢谢..

public class MainActivity extends AppCompatActivity {
Random random = new Random();
int number = random.nextInt(20);
int tries = 5;

TextView result;
EditText confirm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
result = findViewById(R.id.textView);
confirm = findViewById(R.id.editTextTextPersonName);
}

public void onConfirmClick (View view){
    int pass = Integer.parseInt(confirm.getText().toString());
    if (tries >= 0) {
        confirm.setText("Sorry your chances are over. Please retry");
    } else {
        if (pass != number) {
            tries = tries - 1;
            confirm.setText("That's kinda wrong. chances remaining: " + tries);

        } else if (pass == number) {
            confirm.setText("NICE. your answer is correct!. press retry to play again.");

        } else {
            confirm.setText("Don't you understand what a number is?");
        }

    }

    }

public void onRetryClick (View view){
    number = random.nextInt(20);
    tries = 5;
    }
}