Home > Net >  Error in java and xml files in Android Studio
Error in java and xml files in Android Studio

Time:10-08

I'm getting an error in Android studio.

The error in my xml file says: "Unexpected text found in layout file "android:id="@ id/idLLScore"> It's on row 6!

The error in my Java file says:"Cannot resolve symbol 'idLLScore'". And it's on row 88!

I'm not sure how I can fix this? Here is my code:

Java file

package se.hv.ei.quizapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    String question;
    private TextView questionTV,questionNumberTV;
    private Button option1Btn,option2Btn,option3Btn,option4Btn;
    private ArrayList<QuizModal> quizModalArrayList;
    Random random;
    int currentScore = 0, questionAttempted = 1, currentPos;
    private Object LinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);
        quizModalArrayList = new ArrayList<>();
        random = new Random();
        getQuizQuestion(quizModalArrayList);
        currentPos = random.nextInt(quizModalArrayList.size());
        setDataToViews(currentPos);
        option1Btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if(quizModalArrayList.get(currentPos).getAnswer().trim().toLowerCase().equals(option1Btn.getText().toString().trim().toLowerCase())){ currentScore  ; }
        questionAttempted  ;
        currentPos = random.nextInt(quizModalArrayList.size());
        setDataToViews(currentPos);
            }
        });

        option2Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(quizModalArrayList.get(currentPos).getAnswer().trim().toLowerCase().equals(option2Btn.getText().toString().trim().toLowerCase())){ currentScore  ;
                }
                questionAttempted  ;
                currentPos = random.nextInt(quizModalArrayList.size());
                setDataToViews(currentPos);
            }
        });

        option3Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(quizModalArrayList.get(currentPos).getAnswer().trim().toLowerCase().equals(option3Btn.getText().toString().trim().toLowerCase())){ currentScore  ;
                }
                questionAttempted  ;
                currentPos = random.nextInt(quizModalArrayList.size());
                setDataToViews(currentPos);
            }
        });

        option4Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(quizModalArrayList.get(currentPos).getAnswer().trim().toLowerCase().equals(option4Btn.getText().toString().trim().toLowerCase())){ currentScore  ;
                }
                questionAttempted  ;
                currentPos = random.nextInt(quizModalArrayList.size());
                setDataToViews(currentPos);
            }
        });
    }


    private void showButtonSheet(){
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
        View bottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.score_bottom_sheet,(LinearLayout)findViewById(R.id.idLLscore));
        TextView scoreTV = bottomSheetView.findViewById(R.id.idTVScore);
        Button restartQuizBtn = bottomSheetView.findViewById(R.id.idBtnRestart);
        scoreTV.setText("Your Score is \n" currentScore   "/10");
        restartQuizBtn.setOnClickListener((new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPos = random.nextInt(quizModalArrayList.size());
                setDataToViews(currentPos);
                questionAttempted = 1;
                currentScore = 0;
                bottomSheetDialog.dismiss();
            }
        }));
        bottomSheetDialog.setCancelable(false);
        bottomSheetDialog.setContentView(bottomSheetView);
        bottomSheetDialog.show();
    }


    private void setDataToViews(int currentPos){
           questionNumberTV.setText("Questions Attempted : "  questionAttempted   "/10");
           if(questionAttempted == 10){
               showButtonSheet();
           }else{
               questionTV.setText(quizModalArrayList.get(currentPos).getQuestion());
               option1Btn.setText(quizModalArrayList.get(currentPos).getOption1());
               option2Btn.setText(quizModalArrayList.get(currentPos).getOption2());
               option3Btn.setText(quizModalArrayList.get(currentPos).getOption3());
               option4Btn.setText(quizModalArrayList.get(currentPos).getOption4());
           }
    }

    private void getQuizQuestion(ArrayList<QuizModal> quizModalArrayList) {
            quizModalArrayList.add(new QuizModal( "If a cat swishes its tail about, what are they trying to tell you?", "They're happy",  "They're bored", "They're annoyed",  "That they just farted", "They're annoyed"));
            quizModalArrayList.add(new QuizModal( "How many hours do cats sleep per day?", "4 hours", "20 hours",  "14 hours", "9 hours", "20 hours"));
            quizModalArrayList.add(new QuizModal( "Cat tongues are?",  "Shrivelled", "Sticky",  "Smooth",  "Rough",  "Rough"));
            quizModalArrayList.add(new QuizModal(  "Cats sweat from which part of their body?",  "Their paws",  "Their armpits",  "Their tongue",  "Their tail",  "Their paws"));
    }
}

My XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    android:id="@ id/idLLScore">

    <TextView
        android:id="@ id/idTVScore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/purple_200"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textAllCaps="false"
        android:textAlignment="center"
        android:gravity="center"
        android:text="@string/your_score_is"
        android:layout_margin="10dp"
        android:padding="3dp"
    />

    <Button
        android:id="@ id/idBtnRestart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/restart_quiz"
        android:textAllCaps="false"
        />


</LinearLayout>

What am I doing wrong? Thankful for help or guidance!

CodePudding user response:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    android:id="@ id/idLLScore">

You've got an extra character on the row above

android:layout_height="wrap_content"> <<<<<<

That's why it won't compile your XML and then why your java file can't find the reference.

Aka it should be like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@ id/idLLScore">
  • Related