2010-03-30 125 views
0

当我使用的静态SQLite数据库类中发生异常时,我想向用户(msg框或Toast)显示一个msg。在静态SQLiteDatabase类中调用非静态方法

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

这是类

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

回答

2

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

Context context = aContext; 
CharSequence text = "Hello toast!"; 
int duration = Toast.LENGTH_SHORT; 

Toast toast = Toast.makeText(context, text, duration); 
toast.show(); 
+0

谢谢,但我有问题,在一个线程中创建它你有什么想法我怎么处理它 – Jimmy 2010-04-02 03:17:07

相关问题