简体   繁体   English

Android应用程式当机NullPointerException

[英]Android App Crash NullPointerException

I've done the searching and it seems to be a case by case basis as to what's causing this. 我已经进行了搜索,这似乎是由案例决定的。 Though, someone who truly understands it will quite possibly disagree. 但是,真正了解它的人很可能会不同意。

My buttons going from my main screen into other activities work just fine, the only difference with the one that doesn't work is that it's actually pulling a class/variable from another .java file and so I'm sure I haven't implemented it correctly. 从主屏幕进入其他活动的按钮可以正常工作,与不起作用的按钮唯一的区别在于,它实际上是从另一个.java文件中拉出一个类/变量,因此我确定我没有实现正确。

It's a Singleton class: 这是单例课程:

GLOBAL.JAVA GLOBAL.JAVA

//Constructor 
//
protected Global() {
    getInstance();
}


//Singleton Method
//
public static Global getInstance() {
    if(instance == null) {
        initialize();
        instance = new Global();
    }
    return instance;
}

I have a few (static)class structures(I use the term structures because I program primarily in C++, and to me, that's what they are) that are placed inside the Global file/class, along with this array that holds all the data for the program static questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS]; 我有一些(静态)类结构(我使用术语结构是因为我主要使用C ++进行编程,对我来说就是这样),它们都放置在Global文件/类中,并带有保存所有数据的数组。对于该程序, static questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS];

The initialize(); initialize(); function called from the getInstance() (Singleton Method) simply fills in the data into the questionStruct[] array. getInstance() (Singleton方法)调用的函数只是将数据填充到questionStruct[]数组中。

I have a method to return a string 我有一个返回字符串的方法

public static String getQ(int q){

    return questions[q].q;
}

In the .java file that gets called from the button pressed, I have placeData(); 在通过按下按钮调用的.java文件中,我有placeData(); just to test things out, and in that function: 只是为了测试,并在该功能中:

public void placeData() {

    String testString = ("Testing Text: " + Global.getQ(3));

    TextView Q = (TextView) findViewById(R.id.testingText);
    Q.setText(testString);


}

and lastly... hopefully... the TextView where it's supposed to show up in the XML file: 最后...希望...应该在XML文件中显示的TextView:

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

And here's my official error I believe is the indicator to the crash, that only occurs when I press the button to initialize this activity. 这是我的官方错误,我相信是崩溃的指示器,仅当我按下按钮来初始化此活动时才会发生。

java.lang.RuntimeException: Unable to start activity ComponentInfo{godwin.com.study/godwin.com.study.practice}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String godwin.com.study.Global$questionStruct.q' on a null object reference

Thoughts??? 思考???

EDIT: 编辑:

Classes (like a structure in C++) inside of Global.Java Global.Java内部的类(如C ++中的结构)

class answerStruct
{
    String a;
    boolean status;
}


//Structure for Questions
//
class questionStruct
{
    String q;

    answerStruct[] answer = new answerStruct[TOTAL_ANSWERS];

}

The "initialize()" function: “ initialize()”函数:

public static void initialize() {

    questions[0].q = "Question # 1";
    questions[0].answer[0].a = "Q1A1";
    questions[0].answer[0].status = true;
    questions[0].answer[1].a = "Q1A2";
    questions[0].answer[1].status = false;

    ...And so on...Forever
}

When you allocate an array of objects in Java like this: 当您像这样在Java中分配对象数组时:

static questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS];

You are only allocating an array of object references, you aren't actually allocating the objects themselves. 您仅分配对象引用的数组,实际上并没有分配对象本身。 It's kind of like you allocated an array of pointers in C++, but they don't point to anything yet. 有点像您在C ++中分配了一个指针数组,但是它们还没有指向任何东西。

At the top of your initialize() function, you can allocate all the questionStruct objects, and also the answers for each in a nested for loop, like this: 在initialize()函数的顶部,您可以在嵌套的for循环中分配所有questionStruct对象以及每个对象的答案,如下所示:

for(int i = 0; i < TOTAL_QUESTIONS; i++)
{
    questions[i] = new questionStruct();
    for(int j = 0; j < TOTAL_QUESTIONS; j++)
        questions[i].answer[j] = new answerStruct();
}

Then you can go through and initialize all the fields in the structs as you are doing. 然后,您可以按照自己的方式遍历并初始化结构中的所有字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM