Displaying Toast notifications from Java class

I want to create a separate class in my application to handle error reporting and send specific errors to the database. However, I cannot figure out what should be Context

and how it should be correctly coded. I guess it should be possible, I just need to code it differently, if not, 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 am trying to do is create one class for my entire application that handles writing SQL errors when posting data to the database. The class should do 2 simple things. Submit information based on which form, database, time and SQL that generated the error. Another thing I would like to do to this class is to show a toast that returns basic error information to the user. I have a data view part that was designed correctly (hence the reason for Runnable

), but I am still getting errors for Toast.

+3


source to share


3 answers


You shouldn't do the work in your constructor, it 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 should be the appropriate context: from is Toast

Context

usually found Activity

, which can be of the form:



  • this

    (in Activity

    )
  • ActivityName.this

    (in inner class Activity

    )
  • getActivity

    ( Fragment

    inside Activity

    )

For example:

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

      

+2


source


It just needs to be passed Context

in the constructor when creating this class.



I would advise you to rethink this class, although it is called "SendError" which sounds like the name of a method it implements for some reason Runnable

and it notifies the user with Toasts - sounds like too much for one class.

+1


source


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

      

or

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

      

+1


source







All Articles