我正在尝试使用游标适配器,但是当我调用onPostExecute时出现了空指针异常,我无法弄清楚问题是什么。活动的主要思想是显示联系人列表。码:
主要活动:
public class AddressBook extends ListActivity {
public static final String ROW_ID = "row_id"; // additional Intent key
private ListView contactListView; // ListView component from ListActivity
private CursorAdapter contactAdapter; //CursorAdapter for ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // access to ListView
contactListView.setOnItemClickListener(viewContactListener);
// display contact name on TextView in ListView
String[] from = new String[]{"name"};
int[] to = new int[]{R.id.contactTextView};
CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to);
setListAdapter(contactAdapter); // setting adaptercontactView
}
// creating Activity menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addressbook_menu, menu);
return true;
}
// Selectng option
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//create new Intent object for AddEditContact method
Intent addNewContact =
new Intent(AddressBook.this, AddEditContact.class);
startActivity(addNewContact); // start the AddEditContact Activity
return super.onOptionsItemSelected(item); // call superclass method
}
@Override
protected void onResume() {
super.onResume();
// Create new GetContactsTask object and call it
new GetContactsTask().execute((Object[]) null);
}
@Override
protected void onStop() {
Cursor cursor = contactAdapter.getCursor(); // access to current Cursor
if (cursor != null)
cursor.deactivate(); // deactivating
contactAdapter.changeCursor(null); // change Cursor to null
super.onStop();
}
private class GetContactsTask extends AsyncTask<Object, Object, Cursor> {
DatabaseConnector databaseConnector =
new DatabaseConnector(AddressBook.this); // performing access to database
@Override
protected Cursor doInBackground(Object... params) {
databaseConnector.open(); // access to Cursor
return databaseConnector.getAllContacts();
}
@Override
protected void onPostExecute(Cursor result) { // using Cursor, returned by doInBackground method
contactAdapter.changeCursor(result); // change Cursor adapter //NULL POINTER EXCEPTION(ContactAdapter is null)
databaseConnector.close();
}
}
}
LogCat:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.va.testappadr.AddressBook$GetContactsTask.onPostExecute(AddressBook.java:92)
at com.va.testappadr.AddressBook$GetContactsTask.onPostExecute(AddressBook.java:77)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)...
最佳答案
问题是您通过执行以下操作来隐藏游标适配器:
CursorAdapter contactAdapter = new ...
在onCreate方法中...因此,异步任务正在处理从未初始化的对象...。
为什么?
因为您要在onCreate中声明一个新对象
...而是使用global并将其初始化...
在
onCreate
中更改代码... // display contact name on TextView in ListView
String[] from = new String[]{"name"};
int[] to = new int[]{R.id.contactTextView};
//HERE!!
contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to);
setListAdapter(contactAdapter); // setting adaptercontactView
}