简体   繁体   English

显示Java类的Toast通知

[英]Displaying Toast notifications from Java class

I want to create a separate class within my application to handle error reporting and send specific errors to a database. 我想在我的应用程序中创建一个单独的类来处理错误报告并将特定错误发送到数据库。 However, I'm unable to figure out what the Context should be and how this should be properly coded. 但是,我无法弄清楚Context应该是什么以及如何正确编码。 I assume it should still be possible, I just need to code it differently, if that is not the case, what is the best solution for me? 我认为它应该仍然可行,我只需要以不同的方式编码,如果不是这样,对我来说最好的解决方案是什么?

public class SendError implements Runnable
{

    private String url;

    public SendError(String errors, String form, String database, String SQL)
    {
        url = string;

        Handler handler = new Handler();        
        handler.post(new Runnable() {
            public void run() {
                Toast toast = Toast.makeText(getContext, msg, Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }
}

EDIT: 编辑:

What I'm trying to do is create one class for my entire application that handles recording of SQL errors when submitting data to the database. 我要做的是为我的整个应用程序创建一个类,在向数据库提交数据时处理SQL错误的记录。 The class needs to do 2 simple things. 这堂课需要做两件简单的事情。 Submit information based on what form, database, time submitted, and the SQL code that created the error. 根据提交的表单,数据库,时间以及创建错误的SQL代码提交信息。 The other thing I would like this class to do is to display a toast giving basic error information back to the user. 我希望这个类做的另一件事是显示一个toast,将基本的错误信息反馈给用户。 I have the data submission portion of this worked out properly (hence the reason for the Runnable ), but am still getting errors for the Toast. 我有正确的数据提交部分(因此Runnable的原因),但仍然得到Toast的错误。

Shouldn't do the work in your constructor, this makes your separate class useless. 不应该在构造函数中完成工作,这会使您的单独类无用。

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

Your context needs to be the relevant context, from using a Toast the Context is usually an Activity which can take the form of: 您的上下文需要是相关的上下文,从使用ToastContext通常是一个Activity ,它可以采取以下形式:

  • this (in an Activity ) this (在Activity
  • ActivityName.this (in a inner class of an Activity ) ActivityName.this (在内部类的的Activity
  • getActivity (in a Fragment inside an Activity ) getActivity (在Activity内的Fragment

For example: 例如:

 new SendError(YourActivity.this, "something").makeToast("Hello", "errors", "form", "database", "sql");

Just need to pass Context in the constructor when you create this class. 只需要在创建此类时在构造函数中传递Context

I would advise you rethink this class though - it's called "SendError" which sounds like a method name, it implements Runnable for some reason, and it's notifying the user with Toasts - sounds like too much for one class. 我建议你重新考虑这个类 - 它被称为“SendError”,它听起来像一个方法名称,它出于某种原因实现了Runnable ,并且它通过Toasts通知用户 - 听起来对于一个类来说太多了。

Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

要么

Toast toast = Toast.makeText(SendError.this, msg, Toast.LENGTH_LONG).show();

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

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