【问题标题】:Call a non static methode in a static SQLiteDatabase class在静态 SQLiteDatabase 类中调用非静态方法
【发布时间】:2010-03-30 16:41:42
【问题描述】:

当我使用的静态 SQLite 数据库类中发生异常时,我想向用户显示一条消息(消息框或 Toast)。

问题是我不能在静态类中调用非静态方法,我该如何处理。

这是类

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();

【问题讨论】:

    标签: java android exception-handling


    【解决方案1】:

    听起来您正在尝试使用“getApplicationContext()”函数,这是一个非静态方法。您不能从静态方法调用非静态方法。为什么不直接使用传入的上下文?即,

    Context context = aContext;
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;
    
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    

    【讨论】:

    • 谢谢,但我在线程中创建它有问题你知道我该如何处理它
    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多