获取FATAL EXCEPTION:创建对象时的主java.lang.StackOverflowError

时间:2014-09-02 10:14:38

标签: java android

我有一个名为GetQuestions的类,我想在其中创建一个对象,以便稍后将值赋值给它的属性,如下所示:

public class GetQuestions implements Serializable {

public int questionNumber = 1;
Question question =  new Question();

public String[] mCategories = new String[]{
        "Geografia*",
        "Matematicas",
        "Arte",
        "Ciencia",
        "Ocio",
        "Deportes",
        "Historia"

};
public String[] mSomeQuestions = new String[]{
        "Cuantgo es 2 mas dos*",
        "Que edad tengo",
        "Pregunta random",
        "Que edad tienes",
        "Y si...?",
        "Aaron es tonto?",
        "Ivan es mas tonto que Aaron? "

};

public void getNewQuestion(){
    int idx = new Random().nextInt(mCategories.length);
    question.mCategory=mCategories[idx];
    question.mDescription=mSomeQuestions[idx];
    question.setArrayofAnswers("Some Answer","This Answer","No, that answer","Could it be this one?");

}

public void plusOne(){
    this.questionNumber++;
}


 }

这是课程问题:

public class Question extends GetQuestions implements Serializable{


public String mCategory;
public String mDescription;
public String answer;
public ArrayList<Integer> mAlr =  new ArrayList<Integer>();
public ArrayList<String> mAnswers = new ArrayList<String>();

public int asignButtons(){
    int idx = new Random().nextInt(mAnswers.size());
    if(mAlr.contains(idx)){
        while (mAlr.contains(idx)) {
            idx = new Random().nextInt(mAnswers.size());
        }
    }

   answer=mAnswers.get(idx);
    return idx;
}

public void setArrayofAnswers( String mAnswer1, String mAnswer2, String mAnswer3,String mAnswer4){
    mAnswers.add(mAnswer1);
    mAnswers.add(mAnswer2);
    mAnswers.add(mAnswer3);
    mAnswers.add(mAnswer4);
}

  }

但是我得到了这个错误,有些帮助吗?我不熟悉Java和Android,所以我不知道自己做错了什么。

09-02 11:06:20.168  17036-17036/es.hol.gustavo.testchanging D/OpenGLRenderer﹕ Enabling debug mode 0
09-02 11:06:22.865  17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ threadid=1: stack overflow on call to Les/hol/gustavo/testchanging/GetQuestions;.<init>:V
09-02 11:06:22.865  17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ method requires 36+20+8=64 bytes, fp is 0x41a75314 (20 left)
09-02 11:06:22.866  17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ expanding stack end (0x41a75300 to 0x41a75000)
09-02 11:06:22.866  17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ Shrank stack (to 0x41a75300, curFrame is 0x41a7aec4)
09-02 11:06:22.866  17036-17036/es.hol.gustavo.testchanging D/AndroidRuntime﹕ Shutting down VM
09-02 11:06:22.866  17036-17036/es.hol.gustavo.testchanging W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41b15d40)
09-02 11:06:22.927  17036-17036/es.hol.gustavo.testchanging E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: es.hol.gustavo.testchanging, PID: 17036
java.lang.StackOverflowError
        at es.hol.gustavo.testchanging.Question.<init>(Question.java:12)
        at es.hol.gustavo.testchanging.GetQuestions.<init>(GetQuestions.java:18)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的问题是Question扩展GetQuestions,每个GetQuestions实例还包含Question个对象。这意味着每次尝试创建Question对象时,都会自动在其中创建另一个对象,从而导致无限堆栈的Question个对象。

您应该删除继承关系或删除包含。我会放弃继承。