[英]Can't acces a public method from a public class outside the class in Android Studio
I'm building a SQlite database for a android quiz app.我正在为 android 测验应用程序构建 SQlite 数据库。 In Helper class I created a public method getAllQuestions(), but for some reason I can't access this method(or any other method in the helper class) in MainActivity.
在 Helper class 中,我创建了一个公共方法 getAllQuestions(),但由于某种原因,我无法在 MainActivity 中访问此方法(或 helper 类中的任何其他方法)。 BTW I imported this class to Main but it doesn't help.
顺便说一句,我将此 class 导入到 Main 但它没有帮助。
this is the code:这是代码:
public class DB_Helper extends SQLiteOpenHelper {公共 class DB_Helper 扩展 SQLiteOpenHelper {
public static final String DATABASE_NAME = "PRO_QUIZ";
public static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public ArrayList getAllQuestions(){公共 ArrayList getAllQuestions(){
ArrayList<QuestionsDataBase> questionsList = new ArrayList<>();
db = getReadableDatabase();
String Projection [] ={
QuestionTable._ID,
QuestionTable.Column_Question,
QuestionTable.Column_Option1,
QuestionTable.Column_Option2,
QuestionTable.Column_Option3,
QuestionTable.Column_Option4,
QuestionTable.Column_Correct_Ans
};
Cursor c = db.query(QuestionTable.Table_Name,Projection,null,null,null,null,null);
if(c.moveToFirst()){
do{
QuestionsDataBase questions = new QuestionsDataBase();
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Question)));
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option1)));
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option2)));
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option3)));
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Option4)));
questions.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.Column_Correct_Ans)));
questionsList.add(questions);
}while(c.moveToNext());
}
c.close();
return questionsList;
}
However, when I type in Main: DB_Helper.但是,当我输入 Main: DB_Helper。 I cant see any methods.
我看不到任何方法。
The method getAllQuestions()
is not static
. getAllQuestions()
方法不是static
。 That means that you need an instance of DB_Helper
to access that method (you get an instance of an object with the keyword new
).这意味着您需要一个
DB_Helper
实例来访问该方法(您将获得一个带有关键字new
的 object 实例)。 So do this:所以这样做:
DB_Helper dbHelper = new DB_Helper();
dbHelper.getAllQuestions();
or alternatively you can declare the method as static this way:或者,您可以通过这种方式将该方法声明为 static:
public static ArrayList getAllQuestions(){
...
}
And then access it as you wanted in the first palce:然后在第一时间根据需要访问它:
DB_Helper.getAllQuestions();
But beware that in this case (doing it static
) you will also need to declare all class variables used in that method as static
too:但请注意,在这种情况下(执行
static
)您还需要将该方法中使用的所有 class 变量声明为static
:
private static SQLiteDatabase db;
Recommended further reading: Difference between Static methods and Instance methods推荐阅读: Static方法和Instance方法的区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.