-1

问题出在 onpostexecute 方法中

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contactListView = getListView();
        contactListView.setOnItemClickListener(viewContactListener);

        String[] from = new String[]{"name"};
        int[] to = new int[]{R.id.contactTextView};
        @SuppressWarnings("deprecation")
        CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this,          
                R.layout.contact_list_item, null, from, to);
        setListAdapter(contactAdapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        new GetContactsTask().execute((Object[])null);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Cursor cursor = contactAdapter.getCursor();
        if(cursor != null)
            cursor.deactivate();
        contactAdapter.changeCursor(null);
        super.onStop();
    }

    private class GetContactsTask extends AsyncTask<Object, Object, Cursor>{
        DatabaseConnector databaseConnector = DatabaseConnector(AddressBook.this);

        @Override
        protected Cursor doInBackground(Object... params) {
            // TODO Auto-generated method stub
            databaseConnector.open();

            return databaseConnector.getAllContacts();
        }

        @Override
        protected void onPostExecute(Cursor result) {
            contactAdapter.changeCursor(result);
            databaseConnector.close();
        }
    }
4

1 回答 1

2

我看到您在 onCreate 上声明了contactAdapter。你有另一个 contactAdapter 作为班级成员吗?我的猜测是肯定的,但是由于您在方法级别(onCreate)声明了一个,因此您的类成员将永远不会被设置。

于 2013-05-07T19:35:03.950 回答