I am having a problem with an Android program I’m working on. The method below is in MyActivity. MyActivity has an inner class InnerClass which extends AsyncTask. In the doInBackground method of InnerClass i search a database. The result of that search is passed to this method processSearchResult. Everything works except the very last line throws a NullPointerException. I tried using getApplicationContext() in place of this while creating the button and it still threw the exception. I debugged it and found the problem is in the android.content.ContextWrapper class. In the getApplicationContext() method is a call mBase.getApplicationContext. The problem is that the variable mBase is null. When using the this keyword there is still a call to this method and mBase is still null. Can anyone tell me why mBase is null? Or if its actually normal for mBase to be null?
public void processSearchResult(ResultSet result) {
try {
int x = 1;
while (result.next()) {
String name = result.getString(1);
String ing = result.getString(2);
String ins = result.getString(3);
String notes = result.getString(4);
String type = result.getString(5);
String course = result.getString(6);
Recipe r = new Recipe(name, ing, ins, notes, type, course);
recipeList.add(r);
Button button = new Button(this);
You can’t update the user interface from the
doInBackgroundmethod of theAsyncTaskyou need topublishProgressand update it from theonProgressUpdatemethod instead.How do I publish my progress from
doInBackgroundWhere do I update the UI from then?
this may not refer to what you’re thinking.
What I do is keep a private global
Contextvariable in my code for future reference as in: