【发布时间】:2014-09-02 10:14:37
【问题描述】:
我有一个名为 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)
如果你能提供帮助,谢谢。
【问题讨论】:
-
hmmmm GetQuestions 是 Question 的基类 ...因此,对于每个 Question 类,您都可以创建另一个 Quetion 实例作为字段...这就是您获得 SO Exception line:
Question question = new Question();inside GetQuestions class 的方式。 ..无论如何,您的类层次结构似乎设计得不好... -
哦,好吧,我在 Question 类中取出了 extends 并且它工作了,但是我将如何扩展它并在 GetQuestion 中创建对象?因为现在我不需要它,但也许以后我会