I made a class to handle a simple message popup so I can reuse the code throughout the app. I can’t seem to be able to get the context right. This is called from all over the place and often from classes that does not have a UI directly. See the line below…
public class msg {
public void msghand(String message, Exception e) {
{
String s;
if (e != null)
{
s= message + "\n" + e.getLocalizedMessage() + " " + e.toString();
}
else
{
s= message ;
}
new AlertDialog.Builder( getApplicationContext () ) <<<< HERE IS THE PROBLEM
.setMessage(s)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create()
.show();
}
}
}
Is it possible for you to pass the Context in as a parameter?
Where are you performing work without a Context? Services do not have a UI, but still have a Context.
Edit:
You could create a small message service that is statically accessible, and created when your application starts. For example:
Where MessageService is implemented appropriately
Your DBHelper class could use it via