简体   繁体   English

在静态SQLiteDatabase类中调用非静态方法

[英]Call a non static methode in a static SQLiteDatabase class

i want to display a msg to the user (msg box or Toast) when exception happend in a static SQLite Database class that i use. 当我使用的静态SQLite数据库类中发生异常时,我想向用户显示消息(消息框或Toast)。

the problem is that i cant call a non static method in a static class , how can i handle this. 问题是我无法在静态类中调用非静态方法,我该如何处理。

this is the class 这是班

private static SQLiteDatabase getDatabase(Context aContext) {

and i want to add something like this in the class when exception happen but context generates the problem of reference to non static in static class. 我想在发生异常时在类中添加类似的内容,但是context生成引用静态类中的非静态问题。

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

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

It sounds like you're trying to use the "getApplicationContext()" function, which is a nonstatic method. 听起来您正在尝试使用“ getApplicationContext()”函数,这是一种非静态方法。 You can't call nonstatic methods from static ones. 您不能从静态方法中调用非静态方法。 Why don't you just use the context that is passed in? 您为什么不只使用传入的上下文? ie,

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM