0
カーソルアダプタを使用しようとしていますが、onPostExecuteを呼び出すと、NULLポインタ例外がありますが、問題が何であるか把握できません。活動の主なアイデアは連絡先の表示リストです。コード:カーソルアダプタのヌルポインタ例外
主な活動:
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:中
CursorAdapter contactAdapter = new ...
:
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)...
で
あなたの代わりにグローバルを使用して、それを初期化する...のonCreate に新しいオブジェクトを宣言しているので...
行番号92? –