为什么在尝试实例化对象时却无法解决构造函数错误?

时间:2019-12-14 07:45:09

标签: java android

我有一个实现Parcelable的Question类:

public class Question implements Parcelable{

public static final String DIFFICULTY_EASY = "EASY";
public static final String DIFFICULTY_MEDIUM = "Medium";
public static final String DIFFICULTY_HARD = "HARD";
private String question;
private String option1;
private String option2;
private String option3;
private String option4;
private int answerNo;
private String difficulty;

public Question(){}

public Question(String question, String option1, String option2, String option3,
                String option4, int answerNo, String difficulty) {
    this.question = question;
    this.option1 = option1;
    this.option2 = option2;
    this.option3 = option3;
    this.option4 = option4;
    this.answerNo = answerNo;
    this.difficulty = difficulty;
} .....

从扩展SQLiteOpenHelper类的QuizDBHelper类中,我试图使用参数实例化Qusestion对象。像这样:

private void fillQuestionsTable(){
    Question q1 = new Question("A is correct", "A", "B","C","D",1);
    Question q2 = new Question("B is correct", "A", "B","C","D",2);

    Question q3 = new Question("A is correct", "A", "B","C","D",1);
    Question q4 = new Question("B is correct", "A", "B","C","D",2);
    addQuestion(q1);
    addQuestion(q2);
    addQuestion(q3);
    addQuestion(q4);
}

但是我收到一个错误消息:无法解析构造函数问题(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java。 lang.int)。 如何解决?谢谢。

1 个答案:

答案 0 :(得分:1)

您的构造函数指定了7个参数,但您在此处仅为其指定了6个参数:

new Question("A is correct", "A", "B","C","D",1);
                    ^         ^    ^   ^   ^  ^

您忘记了最后一个参数,即difficulty。您应该这样做:

new Question("A is correct", "A", "B","C","D",1, Question.DIFFICULTY_EASY);