不应该在构造函数中完成工作,这会使您单独的类无用.
public class SendError implements Runnable
{
private final Context context;
private final String url;
public SendError(Context context, String string) {
this.context = context;
this.url = string;
}
public void makeToast(String msg, String errors, String form, String database, String SQL) {
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
toast.show();
}
});
}
}
您的上下文需要是相关的上下文,通过使用Toast,上下文通常是一种活动,可以采取以下形式:
>这(在活动中)
> ActivityName.this(在Activity的内部类中)
> getActivity(在Activity的一个片段中)
例如:
new SendError(YourActivity.this, "something").makeToast("Hello", "errors", "form", "database", "sql");