在范围[帮助]中无法访问QuestionActivity类型的封闭实例

时间:2014-08-16 17:07:57

标签: java android

目前正在进行测验应用,并出现这些错误。尝试寻找答案,但没有任何帮助。

Practice.java

public class Practice extends Activity {


public static final String QUIZNUM = "QUIZNUM";
public static final int Quiz1 = 1;
public static final int Quiz2 = 2;
public static final int Quiz3 = 3;
public static final int NUMBER_OF_QUESTIONS = 10;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.practice);

    //Menu buttons
    Button quiz1 = (Button) findViewById(R.id.q1Btn);
    quiz1.setOnClickListener(new PracticeButton(Quiz1, NUMBER_OF_QUESTIONS));

    Button quiz2 = (Button) findViewById(R.id.q2Btn);
    quiz2.setOnClickListener(new PracticeButton(Quiz2, NUMBER_OF_QUESTIONS));

    Button quiz3 = (Button) findViewById(R.id.q3Btn);
    quiz3.setOnClickListener(new PracticeButton(Quiz3, NUMBER_OF_QUESTIONS));

}


class PracticeButton implements OnClickListener {

    int quizNum;
    int numQuestions;

    public PracticeButton(int quizNum, int numQuestions){
        this.quizNum = quizNum;
        this.numQuestions = numQuestions;
    }

    public void onClick(View v){


        List<Question> questions = getQuestionSetFromDb();
        CurrentQuiz c = new CurrentQuiz();
        c.setQuestions(questions);
        c.setNumRounds(numQuestions);
        c.setQuizNum(quizNum);

        ((MLearningApp)getApplication()).setCurrentGame(c);

        startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);
    }

}


// Method that retrieves a random set of questions from db

private List<Question> getQuestionSetFromDb() throws Exception {

    DBHelper myDbHelper = new DBHelper(QuestionActivity.this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Exception("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
    List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);
    myDbHelper.close();
    return questions;
}

}

中收到错误讯息

1 - QuestionActivity.this 在范围内无法访问QuestionActivity类型的封闭实例

startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);

2 - (QuestionActivity.this) 在范围

中无法访问QuestionActivity类型的封闭实例
DBHelper myDbHelper = new DBHelper(QuestionActivity.this);

3 - (quizNum,numQuestions)
     - numQuestions无法解析为a      变量      - quizNum无法解析为      变量

List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);

1 个答案:

答案 0 :(得分:0)

更改此

startActivity(new Intent(QuestionActivity.this, QuestionActivity.class);

startActivity(new Intent(Practice.this, QuestionActivity.class);

阅读

的显式意图

http://developer.android.com/guide/components/intents-filters.html

更改此

DBHelper myDbHelper = new DBHelper(QuestionActivity.this);

DBHelper myDbHelper = new DBHelper(Practice.this);

第三次

int numQuestions; 
//declared in class PracticeButton. not available in  getQuestionSetFromDb(). 

您的PracticeButton课程在getQuestionSetFromDb()之前结束。

getQuestionSetFromDb()课程内移动PractiveButton

无论如何都要掌握教程并阅读文档。