在onpostexecute方法中获取NullPointerException

时间:2013-05-07 19:29:22

标签: android

问题在于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();
        }
    }

1 个答案:

答案 0 :(得分:2)

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