可能重复: How做我做从非活性类举杯?
我如何创建并显示一个吐司
消息不延长活动
类的类?我在由活动
。
How can I create and show a Toast
message from a class which does not extended the Activity
class? I'm using this class in another class that is extended by Activity
.
您需要一个背景参考。您可以在创建类明确作为参数传递
You need a context Reference. you can explicit pass as parameter when you create your class
public class MyClass {
private static Context context;
public MyClass(Context c) {
context = c;
}
public static void showToastMethod() {
Toast.makeText(context, "mymessage ", Toast.LENGTH_SHORT).show();
}
}