android studio中的Java.lang.nullpointerexception [英] Java.lang.nullpointerexception in android studio

查看:81
本文介绍了android studio中的Java.lang.nullpointerexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用琐事应用程序,这是体育类别,随机答案尝试,当我尝试更改为随机位置时,它一直让我的所有按钮都为null在myButtons数组中。



我尝试过:



java工作



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>

推荐答案

findViewById调用可能会为您的按钮返回 null



可以肯定的是,最新进展,改变这个:



The findViewById calls might return null for your buttons.

To be sure, whats going on, change this:

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:

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");





如果你再次启动它,你会看到那些日志输出,你知道,findViewById没有找到你的按钮。是时候清理你的xml了。



欢呼



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


这篇关于android studio中的Java.lang.nullpointerexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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