我得到一个空指针异常,当使用question.settext(arraysportsquestions [num])时崩溃程序; [英] I am getting a null pointer exception that crashes the program when using question.settext(arraysportsquestions[num]);

查看:75
本文介绍了我得到一个空指针异常,当使用question.settext(arraysportsquestions [num])时崩溃程序;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,Question.setText(ArraySportsQuestions [num]);总是返回null并崩溃程序,即使我设置它的文本,我不知道为什么因为我认为我的xml和声明是正确的,请尽快帮助,在2天内到期。对不起,如果我没有提供足够的信息,新的Android工作室,并没有多少教。



In this code below, Question.setText(ArraySportsQuestions[num]); always returns null and crashes the program even though i set it's text, i am not sure why as i think my xml and declaration are correct, please help asap, due in 2 days. Sorry if i am not giving enough info, new to android studio and was not taught much.

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 SportsCat1 extends AppCompatActivity {
    TextView Question;
    Button Choice1;
    Button Choice2;
    Button Choice3;
    Button Choice4;
    TextView Answer;
    TextView SecondsLeft;
    int QuestionCounter =0;

    Integer Points = 0;
    boolean answered = false;
    boolean[] numbers = new boolean[4];

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

        for(int k=0;k<=4;k++)
        {
            Body();
        }
        Intent i = new Intent(getApplicationContext(), MainActivity.class);

        startActivity(i);

    }
         public void Body() {
             Button[] myButtons = {Choice1, Choice2, Choice3, Choice4};
             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?"};
             final String[] ArraySportsQ1 = {"a", "H", "j", "k<b></b>"};
             final String[] ArraySportsQ2 = {"a", "b", "j", "k"};
             final String[] ArraySportsQ3 = {"a", "i", "j", "k"};
             final String[] ArraySportsQ4 = {"a", "ed", "wdd", "fww"};
             final String[] ArraySportsQ5 = {"a", "P", "P", "N"};
             String[][] arrayofQs = {ArraySportsQ1, ArraySportsQ2, ArraySportsQ3, ArraySportsQ4, ArraySportsQ5};

             answered = false;
             QuestionCounter++;
             Random gen = new Random();

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





           Answer = (TextView) findViewById(R.id.Answer);
        SecondsLeft = (TextView) findViewById(R.id.SecondsLeft);
        Question = (TextView) findViewById(R.id.MainQuestion);



        Question.setText(ArraySportsQuestions[num]);

        Choice1.setText(arrayofQs[num][x]);
        Choice2.setText(arrayofQs[num][y]);
        Choice3.setText(arrayofQs[num][z]);
        Choice4.setText(arrayofQs[num][u]);
        new CountDownTimer(10000, 1000) {

            public void onTick(long millisUntilFinished) {
                SecondsLeft.setText("seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                SecondsLeft.setText("Times up!");
                Body();
            }
        }.start();

        Choice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[1]) || Choice1.getText().equals(ArraySportsQ3[1]) || Choice1.getText().equals(ArraySportsQ4[1]) || Choice1.getText().equals(ArraySportsQ5[1]))
                {
                    Toast.makeText(SportsCat1.this, "Correct!" , Toast.LENGTH_LONG).show();
                    Body();}else
                {
                    Toast.makeText(SportsCat1.this, "Incorrect" , Toast.LENGTH_LONG).show();
                    Body();
                }



            }
        });
        Choice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[1]) || Choice1.getText().equals(ArraySportsQ3[1]) || Choice1.getText().equals(ArraySportsQ4[1]) || Choice1.getText().equals(ArraySportsQ5[1]))
                {Points++;
                    Toast.makeText(SportsCat1.this, "Correct!" , Toast.LENGTH_LONG).show();
                    Body();}else
                {
                    Toast.makeText(SportsCat1.this, "Incorrect" , Toast.LENGTH_LONG).show();
                    Body();
                }
            }
        });

        Choice3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[1]) || Choice1.getText().equals(ArraySportsQ3[1]) || Choice1.getText().equals(ArraySportsQ4[1]) || Choice1.getText().equals(ArraySportsQ5[1]))
                {Points++;
                    Toast.makeText(SportsCat1.this, "Correct!" , Toast.LENGTH_LONG).show();
                    Body();}else
                {
                    Toast.makeText(SportsCat1.this, "Incorrect" , Toast.LENGTH_LONG).show();
                    Body();
                }
            }

        });

        Choice4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[1]) || Choice1.getText().equals(ArraySportsQ3[1]) || Choice1.getText().equals(ArraySportsQ4[1]) || Choice1.getText().equals(ArraySportsQ5[1]))
                {Points++;
                    Toast.makeText(SportsCat1.this, "Correct!" , Toast.LENGTH_LONG).show();
                    Body();}else
                {
                    Toast.makeText(SportsCat1.this, "Incorrect" , Toast.LENGTH_LONG).show();
                    Body();
                }
            }

        });











}}



<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"
    android:removed="@mipmap/backgroundsports"
    >


    <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="@string/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" />


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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Question1"
        android:text="@string/q1"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Question2"
        android:text="@string/q2"
        android:layout_gravity="center_horizontal" />

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Choice4"
        android:text="@string/q4"
        android:layout_gravity="center_horizontal" />





我尝试过:



在线检查并询问人



What I have tried:

Checking online and asking people

推荐答案

您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



将断点放在使用数组的位置,并检查数组是否包含您期望的内容以及索引指向的内容现有问题;你可能会感到惊讶。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Put breakpoints where you use the array and check that the array contain what you expect and that the index is pointing to an existing question; You may get a surprise.


这篇关于我得到一个空指针异常,当使用question.settext(arraysportsquestions [num])时崩溃程序;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆