0

I tried to build my app, and this error message was produced.

Unexpected character '=' (code 61) (expected a name start character) at [row,col {unknown-source}]: [41,21]

I have cleaned my code and tried to clean my code and inspected my code and i can't find anything wrong my code, and help would be gratefully recieved.

This is my MainActivity.

 package com.michaeldoughty.android.football_quiz;

 import androidx.activity.result.ActivityResult;
 import androidx.activity.result.ActivityResultCallback;
 import androidx.activity.result.ActivityResultLauncher;
 import 
 androidx.activity.result.contract.ActivityResultContracts;
 import androidx.appcompat.app.AppCompatActivity;

 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.*;

 import java.text.DecimalFormat;


 public class MainActivity extends AppCompatActivity
{
// to answer true for the question
private Button mTrueButton;

// to answer false for the question
private Button mFalseButton;

// to get the next question
private ImageButton mNextButton;

// to cheat
private Button mCheatButton;

// to hold the questions
private TextView mQuestionTextView;

// whether the user is a cheater
private boolean mIsCheater;

// a key used for the onSaveInstanceState
private final String KEY_INDEX = "index";

// An ActivityResultLauncher for the CheatActivity
ActivityResultLauncher<Intent> cheatActivityResultLauncher = 
registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>()
        {
            @Override
            public void onActivityResult (ActivityResult 
 result)
            {
                //checking if there is a result code from the 
 returned intent
                if (result.getResultCode () == RESULT_OK)
                {
                    // to check is there is any data in the 
 returned intent
                    if (result.getData() == null)
                    {
                        return;
                    }

                    // setting that the user is a cheater
                    mIsCheater = true;
                }
            }
        });


// an array of Question objects
private Question [] mQuestionBank = new Question []
        {
            new Question (R.string.question_euro, true),
            new Question (R.string.question_world_cup, false),
            new Question (R.string.question_premier_league, 
 false),
            new Question (R.string.question_england, true),
            new Question (R.string.question_fa_cup, false),
            new Question (R.string.question_league_cup, true)
        };

 // the index of the current question being displayed
 private int mCurrentIndex = 0;

 // to hold the percentage of correct answers
 private double percentage = 0;

 // to hold the amount of correct answers
 private double correct = 0;

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

    // to check if there is a value of mCurrentIndex in the 
 savedInstanceState
    if (savedInstanceState != null)
    {
        mCurrentIndex = savedInstanceState.getInt (KEY_INDEX, 
 0);
    }

    // to create the mQuestionTextView
    mQuestionTextView = (TextView) findViewById 
  (R.id.question_text_view);
    mQuestionTextView.setOnClickListener(new 
  View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            // update the mCurrrentIndex
            mCurrentIndex++;

            // enable the mTrueButton and mFalseButton
            mTrueButton.setEnabled (true);
            mFalseButton.setEnabled (true);

            if (mCurrentIndex < 6)
            {
                // call the updateQuestion method
                updateQuestion();
            }
            else
            {
                displayPercentage ();
                mCurrentIndex = 0;
                updateQuestion();
            }
        }
    });

    // call the updateQuestion method
    updateQuestion ();

    // to create the mTrueButton
    mTrueButton = (Button) findViewById (R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            // call the checkAnswer method
            checkAnswer (true);

            // disable the mTrueButton and mFalseButton
            mTrueButton.setEnabled (false);
            mFalseButton.setEnabled (false);
        }
    });

    // to create the mFalseButton
    mFalseButton = (Button) findViewById (R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            // call the checkAnswer method
            checkAnswer (false);

            // disable the mTrueButton and mFalseButton
            mTrueButton.setEnabled (false);
            mFalseButton.setEnabled (false);
        }
    });

    // to create the mNextButton
    mNextButton = (ImageButton) findViewById 
   (R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            // update the mCurrrentIndex
            mCurrentIndex++;

            // enable the mTrueButton and mFalseButton
            mTrueButton.setEnabled (true);
            mFalseButton.setEnabled (true);

            if (mCurrentIndex < 6)
            {
                // call the updateQuestion method
                updateQuestion();
            }
            else
            {
                displayPercentage ();
                mCurrentIndex = 0;
                updateQuestion();
            }
        }
    });

    // to create the mCheatButton
    mCheatButton = (Button) findViewById (R.id.cheat_button);
    mCheatButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            boolean answerIsTrue = mQuestionBank 
   [mCurrentIndex].isAnswerTrue ();
            Intent intent = CheatActivity.newIntent 
    (MainActivity.this, answerIsTrue);
            cheatActivityResultLauncher.launch (intent);
        }
    });
  }

    /**
   The onSaveInstanceState method saves savedInstanceState when 
  the activity is stopped.
   @param savedInstanceState,  the bundle of the activity.
   */

  @Override
  public void onSaveInstanceState (Bundle savedInstanceState)
 {
    // call the super's constructor
    super.onSaveInstanceState (savedInstanceState);

    // to add the mCurrentIndex to the bundle
    savedInstanceState.putInt (KEY_INDEX, mCurrentIndex);
 }

 /**
   The updateQuestion method updates the question.
 */

 public void updateQuestion ()
 {
    // get the next question
    int question = mQuestionBank [mCurrentIndex].getTextResId 
 ();

    // to display the next question
    mQuestionTextView.setText (question);
 }

 /**
   The checkAnswer method checks the user's answer, to see if 
 its correct or not
   @param userPressedTrue, the user's answer.
 */

 public void checkAnswer (boolean userPressedTrue)
 {
    // getting the correct answer
    boolean answerIsTrue = mQuestionBank 
 [mCurrentIndex].isAnswerTrue ();

    // the message in the toast
    int messageResId = 0;

    // checking the user's answer and setting the right meassge
    if (userPressedTrue == answerIsTrue)
    {
        messageResId = R.string.correct_toast;
        correct++;
    }
    else
    {
        messageResId = R.string.incorrect_toast;
    }

    // displaying the meessage in a toast
    Toast toast;
    toast = Toast.makeText(this, messageResId, 
  Toast.LENGTH_SHORT);
    toast.setGravity (Gravity.TOP|Gravity.CENTER, 0, 0);
    toast.show ();
 }

 /**
   The displayPercentage displays the percentage of correct 
 answers the user had guessed.
 */

 public void displayPercentage ()
 {
    // working out the percentage
    percentage = (correct / mQuestionBank.length) * 100;

    DecimalFormat df = new DecimalFormat("#.00");

    // displaying the percentage as a toast
    Toast.makeText(this, "You have guessed " + 
 df.format(percentage) + "%!", Toast.LENGTH_SHORT).show ();

    // to set the correct and percentage back to zero
    correct = 0;
    percentage = 0;
 }
}

 Here is my CheatActivity

 // an extra key for the intent which tells the CheatActivity 
 what the answer is
 private static final String EXTRA_ANSWER_IS_TRUE = 
 "com.michaeldoughty.android.football_quiz_answer_is_true";

 // an extra key for the intent which tells the MainActivity if 
 the user has cheated
 private static final String EXTRA_ANSWER_IS_SHOWN = 
 "com.michaeldoughty.android.football_quiz_answer_shown";

 // if the answer is true or not
 private boolean mAnswerIsTrue;

 // to hold the correct answer
 private TextView mAnswerTextView;

 // a button to show the correct answer
 private Button mShowAnswerButton;

 // to create an intent to create a CheatActivity
 public static Intent newIntent (Context packageContext, 
 boolean answerIsTrue)
 {
    Intent intent = new Intent (packageContext, 
 CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
 }

 public static boolean wasAnswerShown (Intent result)
 {
    return result.getBooleanExtra (EXTRA_ANSWER_IS_SHOWN, 
 false);
 }

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

    // to get the mAnswerIsTrue
    mAnswerIsTrue = getIntent ().getBooleanExtra 
  (EXTRA_ANSWER_IS_TRUE, false);

    // to create the mAnswerTextView
    mAnswerTextView = findViewById (R.id.answer_text_view);

    // to create the mShowAnswerButton
    mShowAnswerButton = findViewById (R.id.show_answer_button);
    mShowAnswerButton.setOnClickListener(new 
 View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            if (mAnswerIsTrue)
            {
                mAnswerTextView.setText (R.string.true_button);
            }
            else
            {
                mAnswerTextView.setText 
   (R.string.false_button);
            }
            setAnswerShownResult (true);
        }
    });
  }

 /**
   The setAnswerShownResult creates an Intent, to send the 
 isAnswerShown to the MainActivity.
   @param isAnswerShown, whether the user has cheated or not.
 */

  private void setAnswerShownResult (boolean isAnswerShown)
 {
     Intent data = new Intent ();
    data.putExtra (EXTRA_ANSWER_IS_SHOWN, isAnswerShown);
    setResult(RESULT_OK, data);

  }
 }

  Here is my Question class
 package com.michaeldoughty.android.football_quiz;

public class Question
{
// to hold the text res id of the question
private int mTextResId;

// to hold if the answer is true or false
private boolean mAnswerTrue;

/**
   Constructor
*/

public Question (int textResId, boolean answerTrue)
{
    mTextResId = textResId;
    mAnswerTrue = answerTrue;
}

public int getTextResId()
{
    return mTextResId;
}

public void setTextResId(int textResId)
{
    mTextResId = textResId;
}

public boolean isAnswerTrue()
{
    return mAnswerTrue;
}

public void setAnswerTrue(boolean answerTrue)
{
    mAnswerTrue = answerTrue;
}

}

0 Answers0