samedi 22 septembre 2018

Boolean android.widget.checkbox.is checked error

I am working on a quiz that is required to have 4 questions.

Multiple answer questions require checkboxes, single answer questions require a radio button and one text entry. There must also be one if/else statement which I have been having trouble setting up for the checkboxes.

At the end of submission a toast message is to display the total score for all questions answered, for the sake of simplicity I made the quiz to only have the right options as selectable options.

I found a similar question in NullPointerException when using CheckBox in android .

However unlike in that case I have my set content view pointing to my layout activity page.

I am able to get a toast message to appear once I click the submit button however once I tried to run the app on my phone after inputting the check box if statements I receieve the error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.ricardo.quizapp, PID: 23030
                  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference
                      at com.example.ricardo.quizapp.MainActivity$1.onClick(MainActivity.java:62)
                      at android.view.View.performClick(View.java:6205)

Here is my activity main xml code

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<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:textSize="20sp"
        android:layout_height="wrap_content"
        android:text=" Question 1. Name  the main characters in a story about witches and wizards"
        android:id="@+id/question1"
         />

    <CheckBox
       android:id="@+id/checkbox"
        android:text= "Hermione"
        android:layout_marginTop="240dp"
        android:textSize="20sp"
        android:layout_width="wrap_content"


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

    <CheckBox
        android:id="@+id/Harry"
        android:text= "Harry"
        android:layout_marginTop="200dp"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  >  </CheckBox>

    <CheckBox
        android:id="@+id/Ron"
        android:onClick="checked"
        android:layout_marginTop="170dp"
        android:textSize="20sp"
        android:text= "Ron"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  >  </CheckBox>








    <TextView
        android:layout_width="wrap_content"
        android:layout_marginTop="290dp"
        android:textSize="20sp"

        android:layout_height="wrap_content"
        android:text=" Question 2. Who dropped harry off with the dursleys?">


    </TextView>



            <RadioButton
                android:layout_marginTop="400dp"
                android:textSize="20sp"
                android:id="@+id/hagrid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hagrid"
                android:textAppearance="?android:textAppearanceMedium"/>
    <RadioButton
        android:id="@+id/snape"
        android:layout_width="wrap_content"
        android:layout_marginTop="370dp"
        android:textSize="20sp"
        android:clickable="false"
        android:layout_height="wrap_content"
        android:text="snape"
        android:textAppearance="?android:textAppearanceMedium"/>



    <TextView
        android:layout_width="wrap_content"
        android:layout_marginTop="430dp"
        android:textSize="20sp"
        android:layout_height="wrap_content"
        android:text=" Question 3. Who was famous for saying the fan favorite line: Turn to page 394?"
        />
    <RadioButton
        android:layout_marginTop="650dp"

        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hagrid"
        android:clickable="false"

        android:textAppearance="?android:textAppearanceMedium" />


    <RadioButton
        android:layout_marginTop="700dp"

        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"

        android:text="Dumbledore"
        android:textAppearance="?android:textAppearanceMedium" />






    <RadioButton
        android:layout_marginTop="600dp"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Snape"
        android:textSize="20sp"
        android:textAppearance="?android:textAppearanceMedium" />



<TextView
    android:text=" Question 4. Give the last name of the author of the harry potter series lower case spelling:"
    android:layout_width="wrap_content"
    android:layout_marginTop="760dp"
    android:id="@+id/Edit_Text"

    android:textSize="20sp"
    android:layout_height="wrap_content" />


    <EditText
        android:hint="Enter Answer"
        android:layout_marginTop="900dp"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:id="@+id/answer"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:text="Click me to submit the answer"
        android:layout_marginTop="960dp"

        android:id="@+id/button"

        android:layout_height="wrap_content" />
<TextView
    android:layout_width="wrap_content"
    android:id="@+id/rowling"

    android:layout_height="wrap_content" />
      <Button
          android:layout_width="200dp"
          android:layout_centerHorizontal="true"
          android:layout_height="wrap_content"
          android:text="Click me to submit your answers"
          android:layout_marginTop="1200dp"
          android:id="@+id/finalscore"
          android:onClick="finalscore"


          android:textSize="20sp"/>





</RelativeLayout>
</ScrollView>

Java code below

    enter code here
package com.example.ricardo.quizapp;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    TextView Question1;
    Button finalscore;
    CheckBox Hermione;
    CheckBox Harry;
    CheckBox Ron;
    Context context;
    CheckBox sam;
    EditText answer;
    TextView rowling;
    Button submit;
    public Button button;
    String getMystring="the correct answer is hermione, ron, and harry";
    String mystring = "rowling";
    RadioButton hagrid;
    RadioButton snape;
    TextView showfinalscore;
    String string= "you are correct";
    int number =0;
    String stringg= "this is the last score";
    String string2= "this is the wrong choise";


    @Override


    protected void onCreate(Bundle savedInstanceState) {







        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        button =  findViewById(R.id.finalscore);
        button.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View view) {




                if (Hermione.isChecked()==true && Ron.isChecked()==true && Harry.isChecked()== true) {
                    Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
                } else if (Hermione.isChecked()) {
                    Toast.makeText(getApplicationContext(), string2, Toast.LENGTH_LONG).show();
                }}});}}




Aucun commentaire:

Enregistrer un commentaire