我试图弄清楚为什么我的应用无法正常工作

时间:2018-09-25 04:32:05

标签: java xml android-studio

无法终生了解我在做什么错。当我按我的按钮时,应用程序崩溃。如果有人可以帮助解决这个问题,您将感到惊讶。我需要停止该操作,以便可以测试其功能。我的,如果您发现我的代码有其他问题,或者有更好的方法,请随时纠正我。始终欢迎建设性反馈。

这些是它应该具有的所有功能。 https://youtu.be/qAXK2RxIh_s

这是我的代码:

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButton;
    TextView selected_button;
    TextView problem;
    TextView correct_answer;
    EditText user_answer;
    TextView support;

    int counter = 0;

    int high = 0;
    int low = 99;
    int count= 0;
    public int randGen(int low, int high) {
        return (int) Math.round(Math.random() * (high - low)) + low;
    }

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

        radioGroup = findViewById(R.id.radioGroup);

        selected_button = findViewById(R.id.selection);

        problem = findViewById(R.id.equation);

        user_answer = findViewById(R.id.solution);

        correct_answer = findViewById(R.id.answer_check);

        support = findViewById(R.id.encouragement);

        final Button buttonChecked = findViewById(R.id.button_check);
        Button buttonProblem = findViewById(R.id.button_problem);

        buttonProblem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //boolean checked = ((RadioButton) view).isChecked();

                //Timer
                new CountDownTimer(30000,100){
                    public void onTick(long millisUnitilFinished){
                        support.setText(String.valueOf(counter));
                    }

                    public void onFinish(){

                    }
                }.start();

                int radioId = radioGroup.getCheckedRadioButtonId();

                radioButton = findViewById(radioId);

                user_answer.setText(" ");

                //Addition method
                if (radioId == R.id.add){

                    int num1;
                    int num2;
                    int sum;

                    num1 = randGen(0,99);
                    num2 = randGen(0,99);

                    problem.setText(num1 + '+' + num2 + '=');

                    sum = num1 + num2;

                    correct_answer.setText(sum);

                    support.setText(" ");

                }
                //Subtraction method
                if (radioId == R.id.sub){

                    int num1;
                    int num2;
                    int diff;

                    num1 = randGen(0,99);
                    num2 = randGen(0,99);

                    problem.setText(num1 + '+' + num2 + '=');

                    diff = num1 - num2;

                    correct_answer.setText(diff);

                    support.setText(" ");


                }
                //Product method
                if (radioId == R.id.prod){

                    int num1;
                    int num2;
                    int product;

                    num1 = randGen(0,99);
                    num2 = randGen(0,99);

                    problem.setText(num1 + '+' + num2 + '=');

                    product = num1 * num2;

                    correct_answer.setText(product);

                    support.setText(" ");


                }
                //Division Method
                //Should only give problems whose remainder is zero.
                if (radioId == R.id.div){

                    int num1;
                    int num2;
                    int division;

                    num1 = randGen(0,99);
                    num2 = randGen(0,99);

                    problem.setText(num1 + '+' + num2 + '=');

                    division = num1 / num2;

                    correct_answer.setText(division);

                    support.setText(" ");

                }

            }
        });

        buttonChecked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //correct answer
                if(user_answer == (EditText) correct_answer){
                    support.setText("Keep up the good work!");
                    buttonChecked.setEnabled(false);
                }
                //incorrect answer number 2
                else if(count == 1){
                    buttonChecked.setEnabled(false);
                    support.setText("This is the correct answer" + correct_answer + "You suck!");
                    count--;
                }
                //incorrect answer number 1
                else if(user_answer != correct_answer){
                    support.setText("I guess you can try one more time.");
                    count++;
                }
                //If no answer is typed but button is pressed.
                if(user_answer.length()==0){
                    support.setText("ANSWER THE QUESTION!!");

                }


            }
        });

    }
    //Message that lets user know what operation they have selected
    public void checkButton(View v){
        int radioId = radioGroup.getCheckedRadioButtonId();

        radioButton = findViewById(radioId);

        Toast.makeText(this, "You've selected: " + radioButton.getText(), Toast.LENGTH_SHORT).show();
    }
}

这是我的xml:

<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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Pick an Arithmetic Operation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="49dp"
        android:orientation="horizontal"
        android:layout_centerHorizontal="true">

        <RadioButton
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/prod"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="x"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="/"
            android:textAlignment="center"/>
    </RadioGroup>

    <TextView
        android:id="@+id/selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_problem"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-123dp"
        android:text="You've selected: "
        android:textSize="25sp"
        android:visibility="invisible" />

    <Button
        android:id="@+id/button_problem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="123dp"
        android:text="GIVE ME A PROBLEM"
        tools:layout_editor_absoluteX="135dp"
        tools:layout_editor_absoluteY="32dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Type your answer below"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="142dp"
        android:text="CHECK MY ANSWER"
        tools:layout_editor_absoluteX="202dp"
        tools:layout_editor_absoluteY="37dp" />

    <EditText
        android:id="@+id/solution"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="217dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/equation"
        android:layout_width="170dp"
        android:layout_height="52dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="193dp"
        android:text="TextView"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/answer_check"
        android:layout_width="200dp"
        android:layout_height="65dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
        android:text="TextView"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/encouragement"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="108dp"
        android:text="TextView"
        android:visibility="invisible" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在处理“ buttonChecked” onClick事件时,类型转换无效。

if(user_answer == (TextView) correct_answer){ //your code has EditText 
  support.setText("Keep up the good work!");
  buttonChecked.setEnabled(false);
}

“ correct_answer”对象的获取方式为

correct_answer = findViewById(R.id.answer_check);

被声明为“ TextView”。

<TextView
        android:id="@+id/answer_check"
        android:layout_width="200dp"
        android:layout_height="65dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
        android:text="TextView"
        android:visibility="invisible" />