获取致命异常:创建对象时主java.lang.StackOverflower错误
我有一个名为GetQuestions的类,我想在其中创建一个对象,以便以后将值指定给它的属性,如下所示:获取致命异常:创建对象时主java.lang.StackOverflower错误,java,android,Java,Android,我有一个名为GetQuestions的类,我想在其中创建一个对象,以便以后将值指定给它的属性,如下所示: public class GetQuestions implements Serializable { public int questionNumber = 1; Question question = new Question(); public String[] mCategories = new String[]{ "Geografia*", "
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);
}
}
公共类问题扩展GetQuestions实现可序列化{
公共字符串mCategory;
公共字符串描述;
公共字符串应答;
public ArrayList mAlr=new ArrayList();
public ArrayList mAnswers=新的ArrayList();
公共int asignButtons(){
int idx=new Random().nextInt(mAnswers.size());
if(mAlr.contains(idx)){
while(mAlr.contains(idx)){
idx=new Random().nextInt(mAnswers.size());
}
}
答案=mAnswers.get(idx);
返回idx;
}
公共无效设置ArrayOfAnswers(字符串mAnswer1、字符串mAnswer2、字符串mAnswer3、字符串mAnswer4){
添加(mAnswer1);
添加(mAnswer2);
添加(mAnswer3);
添加(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)
09-02 11:06:20.168 17036-17036/es.hol.gustavo.testchanging D/opengl﹕ 正在启用调试模式0
09-02 11:06:22.865 17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ threadid=1:调用Les/hol/gustavo/testchanging/GetQuestions;时堆栈溢出:v
09-02 11:06:22.865 17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ 方法需要36+20+8=64字节,fp为0x41a75314(左20)
09-02 11:06:22.866 17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ 扩展堆栈端(0x41a75300到0x41a75000)
09-02 11:06:22.866 17036-17036/es.hol.gustavo.testchanging I/dalvikvm﹕ 收缩堆栈(到0x41a75300,curFrame为0x41a7aec4)
09-02 11:06:22.866 17036-17036/es.hol.gustavo.testchanging D/AndroidRuntime﹕ 关闭虚拟机
09-02 11:06:22.866 17036-17036/es.hol.gustavo.testchanging W/dalvikvm﹕ threadid=1:线程以未捕获异常退出(组=0x41b15d40)
09-02 11:06:22.927 17036-17036/es.hol.gustavo.testchanging E/AndroidRuntime﹕ 致命异常:主
流程:es.hol.gustavo.testchanging,PID:17036
栈溢出
在es.hol.gustavo.testchanging.Question.(Question.java:12)
在es.hol.gustavo.testchanging.GetQuestions.(GetQuestions.java:18)
谢谢您的帮助。您的问题是
Question
扩展了GetQuestions
,并且每个GetQuestions
实例还包含一个Question
对象。这意味着每次尝试创建一个Question
对象时,都会自动在其中创建另一个对象,从而导致无限多的Question
对象
您应该要么删除继承关系,要么删除包含关系。我会放弃继承。hmmmGetQuestions是问题的基类。。。因此,对于每个问题类,您都会创建另一个Quetion实例作为字段。。。这就是为什么会出现这样的异常行:
Question-Question=new-Question()代码>在GetQuestions类中。。。无论如何,您的类层次结构似乎设计得不好…哦,好吧,我在Question类中进行了扩展,它工作了,但是我如何扩展它并在GetQuestion中创建对象呢?因为现在我不需要它,但也许以后我会谢谢你,正如塞尔文评论我所做的那样,它起了作用。