当我使用的静态 SQLite 数据库类中发生异常时,我想向用户显示一条消息(消息框或 Toast)。
问题是我无法在静态类中调用非静态方法,我该如何处理这个问题。
这是类(class)
private static SQLiteDatabase getDatabase(Context aContext) {
当异常发生时,我想在类中添加类似的内容,但context
会产生静态类中对非静态引用的问题。
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
最佳答案
听起来您正在尝试使用“getApplicationContext()”函数,这是一个非静态方法。您不能从静态方法调用非静态方法。为什么不直接使用传入的上下文呢?即,
Context context = aContext;
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
关于java - 在静态 SQLiteDatabase 类中调用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546947/