Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get a trivia app working and this is the sports category with random answers to try, it keeps giving me null on all of my buttons when i try to change to a random location in the myButtons array.

What I have tried:

java work

package com.example.user.mytriviaapp;



import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.*;
import android.widget.Toast;

public class SportsCat extends AppCompatActivity {
    TextView SecondsLeft;
    TextView Question;
    Button Choice1;
    Button Choice2;
    int num;
    Button Choice3;
    Button Choice4;
    TextView Answer;
    int QuestionCounter =0;
    String[] ArraySportsQuestions = {"Which NBA team holds the record for the most wins in a season in history?","Which of these soccer players has won the most FIFA Ballon d'Ors?",
    "Since what year was table tennis an Olympic sport?","Which of these athletes had a video game made in honour of him?","How many national sports does Canada have?"};
    String[] ArraySportsChoice1 = {"Packers", "H", "j", "pleb","5"};
    String[] ArraySportsChoice2 = {"Packer", "b", "j", "pleb","5"};
    String[] ArraySportsChoice3 = {"Packe", "i", "j", "pleb","5"};
    String[] Answers = {"Packers", "H", "j", "pleb","5"};
    Integer Points = 0;
    boolean answered = false;
    Button[] myButtons = {Choice1,Choice2,Choice3,Choice4};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sportscatsettings);
        Question = (TextView) findViewById(R.id.MainQuestion);
        Choice1 = (Button) findViewById(R.id.Choice1);
        Choice2 = (Button) findViewById(R.id.Choice2);
        Choice3 = (Button) findViewById(R.id.Choice3);
        Choice4 = (Button) findViewById(R.id.Choice4);
        Answer = (TextView) findViewById(R.id.Answer);
        Button[] myButtons = {Choice1,Choice2,Choice3,Choice4};
        while(QuestionCounter < 6)
        {
        Body();}
        Intent g = new Intent(getApplicationContext(), gameDone.class);
        startActivity(g);
    }

        public void Body() {
            answered = false;
            QuestionCounter++;
            Random gen = new Random();
            final int num = gen.nextInt(4);
            int x = gen.nextInt(3);
            int y = gen.nextInt(3);

            if (y == x) {
                y = gen.nextInt(3);
            }
            int z = gen.nextInt(3);
            if (z == x || z == y) {
                z = gen.nextInt(3);
            }
            int u = gen.nextInt();
            if (u == z || u == x || u == y) {
                u = gen.nextInt(3);
            }


            Question.setText(ArraySportsQuestions[num]);
            myButtons[x].setText(ArraySportsChoice1[num]);
            myButtons[y].setText(ArraySportsChoice2[num]);
            myButtons[z].setText(ArraySportsChoice3[num]);
            myButtons[u].setText(Answers[num]);

            Choice1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(SportsCat.this, "Incorrect, the correct answer is " + Answers[num], Toast.LENGTH_LONG).show();
                    answered = true;
                    Body();
                }
            });
            Choice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(SportsCat.this, "Incorrect, the correct answer is " + Answers[num], Toast.LENGTH_LONG).show();
                    answered = true;
                    Body();
                }
            });

            Choice3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(SportsCat.this, "Incorrect, the correct answer is " + Answers[num], Toast.LENGTH_LONG).show();
                    answered = true;
                    Body();
                }
            });
            Choice4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(SportsCat.this, "Correct!", Toast.LENGTH_LONG).show();
                    Points++;
                    answered = true;
                    Body();
                }

            });
        if(answered = false)
        {
            Body();

        }








    }

        }



xml


<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <textview>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/MainQuestion"
        android:layout_gravity="center_horizontal"
        android:text="question" />
    <textview>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/Answer"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Choice1"
        android:text="Q1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Choice2"
        android:text="Q2" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Choice3"
        android:text="q3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Choice4"
        android:text="q4" />
    <textview>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/SecondsLeft"
        android:layout_gravity="center_horizontal" />




</textview></textview></textview></linearlayout>
Posted
Updated 26-May-16 20:40pm
v2
Comments
Richard MacCutchan 27-May-16 3:43am    
Is the above XML in a file named sportscatsettings.xml>

1 solution

The findViewById calls might return null for your buttons.

To be sure, whats going on, change this:

Java
Choice1 = (Button) findViewById(R.id.Choice1);
Choice2 = (Button) findViewById(R.id.Choice2);
Choice3 = (Button) findViewById(R.id.Choice3);
Choice4 = (Button) findViewById(R.id.Choice4);


to this:
Java
Choice1 = (Button) findViewById(R.id.Choice1);
Choice2 = (Button) findViewById(R.id.Choice2);
Choice3 = (Button) findViewById(R.id.Choice3);
Choice4 = (Button) findViewById(R.id.Choice4);
if (Choice1 == null) Log.d("~", "Choice1 is null");
if (Choice2 == null) Log.d("~", "Choice2 is null");
if (Choice3 == null) Log.d("~", "Choice3 is null");
if (Choice4 == null) Log.d("~", "Choice4 is null");


If you start it again and you see those log outputs, you know, that findViewById didn't find your buttons. Time to clean up your xml then.

cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900