Possible Duplicate:
How do I make a toast from a non activity class?
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. Just have a helper method like
public static void showToastMethod(Context context) {
Toast.makeText(context, "mymessage ", Toast.LENGTH_SHORT).show();
}
You can pass context of that activity to your class by passing value to nonActivity class
example:
new NonActivityClass(Activityclass.this) ;
and as in above answer
new MyClass(ActivityClass.this);
In NonActivityClass
public class NonActivityClass {
public NonActivityClass (Context context) {
Toast.makeText(context, "mymessage ", Toast.LENGTH_SHORT).show();
}
}
Hope this works for you...
来源:https://stackoverflow.com/questions/11466799/how-to-display-a-toast-message-in-from-a-class-that-doesnt-extend-activity