Android Nullpointerexception在asynctask中使用游标

Android Nullpointerexception在asynctask中使用游标,android,nullpointerexception,Android,Nullpointerexception,我被这件事搞糊涂了。我跟随书中的一个项目,如果我使用书中的示例文件,这是有效的。然而,我使用的文件在我看来是相同的,我得到了一个空指针异常。以下是文件: //AddressBook.java //Main activity for address book app. package com.deitel.addressbook; import android.app.ListActivity; import android.os.Bundle; import android.os.AsyncT

我被这件事搞糊涂了。我跟随书中的一个项目,如果我使用书中的示例文件,这是有效的。然而,我使用的文件在我看来是相同的,我得到了一个空指针异常。以下是文件:

//AddressBook.java
//Main activity for address book app.
package com.deitel.addressbook;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.database.Cursor;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.util.Log;


public class AddressBook extends ListActivity {

    //string used when logging for debug
    public static final String TAG ="AddressBookBot";

    public static final String ROW_ID = "row_id";  //Intent extra key
    private ListView contactListView;  //the ListActivity's ListView
    private CursorAdapter contactAdapter;  //adapter for ListView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  //call super's onCreate
        Log.d(TAG, "In onCreate() event");
        contactListView = getListView();
        contactListView.setOnItemClickListener(viewContactListener);

        //map each contact's name to a TV in the ListView layout
        String[] from = {"name"};
        int[] to = new int[] {R.id.contactTextView};
        CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this,
                R.layout.contact_list_item, null, from, to);    
        setListAdapter(contactAdapter);  //set contactView's adapter
    }

    @Override
    protected void onResume(){

        Log.d(TAG, "In onResume() event");
        super.onResume();  //call super's onResume method

        //create a new GetContactsTask and execute it
        new GetContactsTask().execute((Object[]) null);
    }  //end onResume

    @Override
    protected void onStop(){

        Log.d(TAG, "In onStop() event");
        Cursor cursor = contactAdapter.getCursor();  //get current Cursor
        if (cursor != null)
            cursor.deactivate();  //deactivate it

        contactAdapter.changeCursor(null);  //adapter now has no Cursor
        super.onStop();
    }  //end onStop

    //performs database query outside GUI thread
    private class GetContactsTask extends AsyncTask<Object, Object, Cursor>{        

        DatabaseConnector DBC = new DatabaseConnector(AddressBook.this);

        //perform the database access
        @Override
        protected Cursor doInBackground(Object... params){
            DBC.open();

            //get a cursor containing call contacts
            return DBC.getAllContacts();
        }  //end method doInBackground

        //use the Cursor returned from the doInBackground method
        @Override
        protected void onPostExecute(Cursor result){
            Log.d(TAG, "In onPostExecute() event");
            contactAdapter.changeCursor(result);  //set the adapter's Cursor
            DBC.close();
        }  //end method onPostExecute
    }  //end class GetContactsTask

    //create the Activity's menu from a menu resource XML file  
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.addressbook_menu, menu);
        return true;
    }  //end onCreateOptionsMenu

    //handle the choice from options menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        //create a new Intent to launch the AddEditContact Activity
        Intent addNewContact = new Intent(AddressBook.this, AddEditContact.class);
        startActivity(addNewContact);  //start the AddEditContact Activity
        return super.onOptionsItemSelected(item);  //call super's method
    }  //end onOptionsItemSelected

    //event listener that responds to the users touching a contact's name in the ListView
    OnItemClickListener viewContactListener = new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){

            //create an Intent to launch the ViewContact Activity
            Intent viewContact = new Intent(AddressBook.this, ViewContact.class);

            //pass the selected contact's row ID as an extra with the intent
            viewContact.putExtra(ROW_ID, arg3);
            startActivity(viewContact);  //start the ViewContact Activity           
        }  //end method onItemClick
    };  //end viewContactListener
}  //end class AddressBook

在此方面的任何帮助或澄清将不胜感激

我认为您的游标对象正在引用为null。result reference变量指向哪个查询?

在getAllContacts()中进行查询之前,您没有打开数据库。 该函数应该如下所示:

public Cursor getAllContacts() {
    Log.d(TAG, "in getAllContacts");

    Cursor cursor = null;

    open();   // open the database
    cursor = database.query("contacts", new String[] {"_id", "name"}, 
                         null, null, null, null, "name");
    close(); // close the database

    return cursor;
}

函数getOneContact()中也存在类似问题。

以下是我做错的地方:

我宣布

private CursorAdapter contactAdapter;
在AddressBook.java文件中。然后以后就不放了

contactAdapter = new SimpleCursorAdapter(AddressBook.this,
                R.layout.contact_list_item, null, from, to);
我当场再次申报

CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this,
                R.layout.contact_list_item, null, from, to); 

这会导致空指针异常。清除第二个declare错误后,应用程序运行正常。再次感谢所有帮助过我的人:)

是的,光标确实引用了null,但我不明白为什么。这里没有什么不对劲的地方。至于结果引用,我非常确定'result'引用的是getAllContacts.doInBackground的返回,调用时会打开数据库,并且(我假设)会给出返回DBC.getAllContacts()的结果;要执行onPostExecute,则应读取设置的光标。这是我对AsyncTask应该如何工作的理解,但我可能不正确。
CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this,
                R.layout.contact_list_item, null, from, to);