我遇到了我正在处理的Android程序的问题。以下方法在MyActivity中。 MyActivity有一个扩展AsyncTask的内部类InnerClass。在InnerClass的doInBackground方法中,我搜索一个数据库。该搜索的结果将传递给此方法processSearchResult。除了最后一行会引发NullPointerException之外,所有工作都会正常工作。我在创建按钮时尝试使用getApplicationContext()代替此操作,但仍抛出异常。我调试了它,发现问题出在android.content.ContextWrapper类中。在getApplicationContext()方法中是调用mBase.getApplicationContext。问题是变量mBase为空。在使用this关键字时,仍然会调用此方法,而mBase仍然为null。任何人都可以告诉我为什么mBase为空?或者,如果它实际上正常的mBase为空?创建新按钮
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);
残缺码 –
MBASE?代码中的mBase在哪里? – Niranjan
如上所述,mBase位于android.content.ContextWrapper类中。 –