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. 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. 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. The other thing I would like this class to do is to display a toast giving basic error information back to the user. 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.
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:
this (in an Activity) ActivityName.this (in a inner class of an Activity) getActivity (in a Fragment inside an Activity)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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With