简体   繁体   English

游标适配器中的空指针异常

[英]Null-pointer exception in Cursor Adapter

I'm trying to use the cursor adapter but I have a null pointer exception when I call onPostExecute , I can't figure out what the problem is. 我正在尝试使用游标适配器,但是当我调用onPostExecute时出现了空指针异常,我无法弄清楚问题出在哪里。 Main idea of activity is display list of contacts. 活动的主要思想是显示联系人列表。 Code: 码:

Main activity: 主要活动:

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: 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)...

The problem is that you are shadowing the cursor adapter by doing this: 问题是您通过执行以下操作来隐藏游标适配器:

CursorAdapter contactAdapter = new ...

in the onCreate method... hence the Async task is manipulating an object that was never initialized.... 在onCreate方法中...因此,异步任务正在处理从未初始化的对象。

Why? 为什么?

because you are declaring a new object in the onCreate ... use the global instead and initialize it... 因为您要在onCreate中声明一个新对象...请改用global并将其初始化...

in the onCreate change the code... 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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM