Home > other >  Having issues with a simple Quiz in Java & Android Studio
Having issues with a simple Quiz in Java & Android Studio

Time:10-06

I've only started writing my code and already I'm having errors. I follow a tutorial for this part, and I've written the exact same code!

I'm working in Android Studio with Java. I have three files. First one is the main file, second one is the modal file, third one is just the xml file.

This is the code in the Main:

package se.hv.ei.quizapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private TextView questionTV,questionNumberTV;
    private Button option1Btn,option2Btn,option3Btn,option4Btn;
    private ArrayList<QuizModal> quizModalArrayList;
    Random random;
    int currentScore = 0, questionAttempted = 1, currentPos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        questionTV = findViewById(R.id.idTVQuestion);
        questionNumberTV = findViewById(R.id.idTVQuestionAttempted);
        option1Btn = findViewById(R.id.idBtnOption1);
        option2Btn = findViewById(R.id.idBtnOption2);
        option3Btn = findViewById(R.id.idBtnOption3);
        option4Btn = findViewById(R.id.idBtnOption4);
        quizModalArrayList = new ArrayList<>();
        random = new Random();
        getQuizQuestion(quizModalArrayList);
    }

    private void getQuizQuestion(ArrayList<QuizModal> quizModalArrayList) {
        quizModalArrayList.add(new QuizModal(question: "G", option1: "t", option2: "s", option3: "d", option4: "a", answer: "a"));
        quizModalArrayList.add(new QuizModal( question: "fff?", option1: "tgooo, option2: "to learn new languages", option3: "to learn android", option4: "all", answer: "all"));
        quizModalArrayList.add(new QuizModal( question: "ggg?", option1: "tpooooo", option2: "to learn new languages", option3: "to learn android", option4: "all", answer: "all"));
        quizModalArrayList.add(new QuizModal( question: "hhh?", option1: "doooo, option2: "to learn new languages", option3: "to learn android", option4: "all", answer: "all"));


    }
}

This is the code in the QuizModal:

package se.hv.ei.quizapp;

public class QuizModal {
    private String question;
    private String option1;
    private String option2;
    private String option3;
    private String option4;
    private String answer;

    public QuizModal(String question, String option1, String option2, String option3, String option4, String answer) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.answer = answer;
    }


    public String getQuestion() {

        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getOption1() {
        return option1;
    }

    public void setOption1(String option1) {
        this.option1 = option1;
    }

    public String getOption2() {
        return option2;
    }

    public void setOption2(String option2) {
        this.option2 = option2;
    }

    public String getOption3() {
        return option3;
    }

    public void setOption3(String option3) {
        this.option3 = option3;
    }

    public String getOption4() {
        return option4;
    }

    public void setOption4(String option4) {
        this.option4 = option4;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

And lastly, this is my xml file:

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Number of Questions"
        android:textAllCaps="false"
        android:textAlignment="center"
        android:gravity="center"
        android:layout_marginTop="50dp"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:id="@ id/idTVQuestionAttempted"
        />

    <TextView
        android:id="@ id/idTVQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/idTVQuestionAttempted"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Question"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="25sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/idTVQuestion"
        android:orientation="vertical">

        <Button
            android:id="@ id/idBtnOption1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/purple_200"
            android:padding="4dp"
            android:text="Option 1"
            android:textAllCaps="false" />


        <Button
            android:id="@ id/idBtnOption2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/purple_200"
            android:padding="4dp"
            android:text="Option 2"
            android:textAllCaps="false" />


        <Button
            android:id="@ id/idBtnOption3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/purple_200"
            android:padding="4dp"
            android:text="Option 3"
            android:textAllCaps="false" />


        <Button
            android:id="@ id/idBtnOption4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/purple_200"
            android:padding="4dp"
            android:text="Option 4"
            android:textAllCaps="false" />

    </LinearLayout>
</RelativeLayout>

I'm getting following errors: "Cannot resolve symbol 'TextView'", "Cannot resolve symbol 'Button'", "Cannot resolve symbol 'ArrayList'", "Cannot resolve symbol 'Random'", "Cannot resolve method 'add(se.hv.ei.quizzapp.QuizModal)', "Cannot resolve symbol 'question'", "Cannot resolve symbol 'option1'", "Variable learn is already defined in the scope"..

Thankful for any help or guidance.. I'm so stuck.

CodePudding user response:

Looks like you missed (") after word "tgooo" in this line:

quizModalArrayList.add(new QuizModal( question: "fff?", option1: "tgooo, option2: "to learn new languages", option3: "to learn android", option4: "all", answer: "all"));

Also try to import some libraries for TextView, Button, Random and ArrayList like import java.util.ArrayList for ArrayList and java.util.Random for Random (i don`t know in which libraries TextView and Button contained, i don't work with Android, sorry)

CodePudding user response:

Edit your code in onCreate:

TextView questionTV = 
findViewById(R.id.idTVQuestion);
     TextView   questionNumberTV = 
findViewById(R.id.idTVQuestionAttempted);
     Button   option1Btn = 
findViewById(R.id.idBtnOption1);
     Button   option2Btn = 
findViewById(R.id.idBtnOption2);
      Button  option3Btn = 
findViewById(R.id.idBtnOption3);
        Button option4Btn = 
findViewById(R.id.idBtnOption4);
  • Related