Android Quiz App在查看用户

时间:2018-05-21 20:48:45

标签: java android xml

我遇到的问题似乎发生在checkIfRight(),我希望它获取单击按钮时的文本,然后检查右边的数据元素的文本,如果它是正确的,则显示"右& #34;如果错了,则显示"错误"即使我在if语句中比较两个字符串,这似乎也不起作用。如果有人能帮助我,我将永远感激不尽。

这是我的java类的下面代码:

package com.example.tass.damnquizinfoapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class Categories extends AppCompatActivity {

int count = 1;
Button btn;
TextView score;
int count2 = 0;
String buttonText;



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



}


String[] quesitons =
        {
                "What was the tallest Damn as of 2017?",
                "How many people did the South Fork Dam kill in 1889?",
                "What do Damns generate?",
                "By how much percent did freshwater species decline by due to dams since 1970?",
                "Around what time was the first damn built?",
                "How many large(15m+) dams are in existence today?",
                "The average dam can be expected to cost how much?"
        };

String[] rightAnswers =
        {
                "Jinping-I",
                "2200",//right
                "Energy",//right
                "37%",
                "4000 B.C",
                "57,000",
                "A few billion dollars"
        };

String[] sampleAnswersA =
        {
                "The Sanxia",
                "2200",//right
                "Energy",//right
                "20%",
                "1280 A.D",
                "7,000",
                "Less than $20million"
        };

String[] sampleAnswersB =
        {
                "Jinping-I",//right
                "204",
                "Fish",
                "68%",
                "4000 B.C",//right
                "57,000",//right
                "More than $1 trillion"
        };

String[] sampleAnswersC =
        {
                "Guri Dam",
                "7819",
                "Water",
                "37%", //right
                "2800 B.C",
                "78,0000",
                "A few billion dollars"//right
        };



public void a1()
{

    btn = (Button) findViewById(R.id.q1);
    btn.setText(sampleAnswersA[count]);



}

public void a2()
{

    btn = (Button) findViewById(R.id.q2);
    btn.setText(sampleAnswersB[count]);


}

public void a3()
{

    btn = (Button) findViewById(R.id.q3);
    btn.setText(sampleAnswersC[count]);
    getQuestions();


}

public void getQuestions()
{
    TextView txt;
    txt = (TextView) findViewById(R.id.mytext);
    txt.setText(quesitons[count]);

}


  //THIS SEEMS TO BE CAUSING ME PROBLEMS 
 public void checkIfRight()

{
    String buttonText;
    buttonText = btn.getText().toString();
    if (rightAnswers[count2].equals(buttonText))
    {
        score = findViewById(R.id.score);
        score.setText("right");
    }

    else{score.setText("wrong");}

}


public void onClick(View v)
{
    a1();
    a2();
    a3();
    checkIfRight();
    getQuestions();
    count++;
    count2++;
}

}

这是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"
tools:context=".MainActivity"
android:background="#e7e7e7"
>

 <ImageView
   android:layout_width="350dp"
   android:layout_height="450dp"
   android:src="@drawable/whitebackground"
   android:layout_centerVertical="true"
   android:layout_centerHorizontal="true"
   />

 <Button
   android:id="@+id/q1"
   android:layout_width="300dp"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:onClick="onClick"
   android:layout_marginTop="180dp"
   android:background="@drawable/rounded_btn"
   android:text="The Sanxia"
   android:textColor="#FFFFFF"
   android:textStyle="bold" />

 <Button
   android:id="@+id/q2"
   android:layout_width="300dp"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="203dp"
   android:onClick="onClick"
   android:background="@drawable/rounded_btn"
   android:text="Jinping-I"
   android:textColor="#FFFFFF"
   android:textStyle="bold" />


 <Button
   android:id="@+id/q3"
   android:layout_width="300dp"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="128dp"
   android:onClick="onClick"
   android:background="@drawable/rounded_btn"
   android:text="Guri Dam"
   android:textColor="#FFFFFF"
   android:textStyle="bold"

   />

 <TextView
   android:id="@+id/mytext"
   android:layout_width="253dp"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="78dp"
   android:text="What was the tallest Damn as of 2017?"
   android:textAlignment="center"
   android:textColor="#000000"
   android:textSize="20dp"
   android:textStyle="bold" />

 <Button
   android:id="@+id/mybutton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="50dp"
   android:text="NEXT"
   android:onClick="onClick"
   />


 <TextView
   android:id="@+id/score"
   android:layout_width="99dp"
   android:layout_height="130dp"
   android:layout_above="@+id/q1"
   android:layout_alignParentEnd="true"
   android:layout_marginBottom="-180dp"
   android:text="Answer:"
   android:textStyle="bold" />

  </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

检查是否正确的方法 你把这条线放在里面 得分= findViewById(R.id.score) 你做到了本地化 所以当执行其他时 它会崩溃 如果我之前建议您将所有行findviewbyid放在oncreate方法中,那就错了。

答案 1 :(得分:0)

我编译了你的代码并得到了同样的错误,我发现你在if语句中初始化了TextView score,只有用户选择right answer才会发生wrong answer所以,请看下面的代码:

package com.example.dosto.categories;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView txt;
    int count = 1;
    Button btn;
    TextView score;
    int count2 = 0;
    String buttonText;

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

        btn = (Button) findViewById(R.id.q1);
        btn = (Button) findViewById(R.id.q3);
        txt = (TextView) findViewById(R.id.mytext);
        btn = (Button) findViewById(R.id.q2);
        score = findViewById(R.id.score);

    }

    String[] quesitons =
            {
                    "What was the tallest Damn as of 2017?",
                    "How many people did the South Fork Dam kill in 1889?",
                    "What do Damns generate?",
                    "By how much percent did freshwater species decline by due to dams since 1970?",
                    "Around what time was the first damn built?",
                    "How many large(15m+) dams are in existence today?",
                    "The average dam can be expected to cost how much?"
            };

    String[] rightAnswers =
            {
                    "Jinping-I",
                    "2200",//right
                    "Energy",//right
                    "37%",
                    "4000 B.C",
                    "57,000",
                    "A few billion dollars"
            };

    String[] sampleAnswersA =
            {
                    "The Sanxia",
                    "2200",//right
                    "Energy",//right
                    "20%",
                    "1280 A.D",
                    "7,000",
                    "Less than $20million"
            };

    String[] sampleAnswersB =
            {
                    "Jinping-I",//right
                    "204",
                    "Fish",
                    "68%",
                    "4000 B.C",//right
                    "57,000",//right
                    "More than $1 trillion"
            };

    String[] sampleAnswersC =
            {
                    "Guri Dam",
                    "7819",
                    "Water",
                    "37%", //right
                    "2800 B.C",
                    "78,0000",
                    "A few billion dollars"//right
            };


    public void a1() {
        btn.setText(sampleAnswersA[count]);
    }

    public void a2() {
        btn.setText(sampleAnswersB[count]);
    }

    public void a3() {
        btn.setText(sampleAnswersC[count]);
        getQuestions();

    }

    public void getQuestions() {
        txt.setText(quesitons[count]);
    }

    public void checkIfRight() {
        String buttonText;
        buttonText = btn.getText().toString();
        if (rightAnswers[count2].equals(buttonText)) {
            score.setText("right");
        } else {
            score.setText("wrong");
        }

    }

    public void onClick(View v) {
        a1();
        a2();
        a3();
        checkIfRight();
        getQuestions();
        count++;
        count2++;
    }
}

所以,请查看这些运行时异常。

此错误来自checkIfRight()enter image description here

修复checkIfRight()后出现此错误,它出现在a1()中: enter image description here

结论是,您需要在onCreate()中初始化视图和按钮,这样您才能获得任何java.lang.IllegalStateException