1

当我尝试执行搜索时,我在日志中收到 NullPointerException。我正在尝试使用 query() 和 getContactMatches() 方法将您从搜索中获得的查询与我的数据库中的联系人姓名相匹配。日志似乎指向这些方法中的代码。由于 getContactMatches() 中使用了 query() 方法,我认为错误源自 query(),但我不知道。

来自数据库类的一些代码:

public Cursor getContactMatches(String query, String[] columns) {
            String selection = COL_NAME + " MATCH ?";
            String[] selectionArgs = new String[] {query+"*"};

            return query(selection, selectionArgs, columns);
        }

private Cursor query(String selection, String[] selectionArgs, String[] columns) {
            SQLiteQueryBuilder build = new SQLiteQueryBuilder();
            build.setTables(DATABASE_TABLE);

            Cursor cursor = build.query(DBHelper.getReadableDatabase(),
                    columns, selection, selectionArgs, null, null, null);

            if (cursor == null) {
                return null;
            }

            return cursor;
        }

搜索活动类的代码:

public class SearchResultsActivity extends ListActivity {

private ListView list;
DatabaseTable db;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_results);
    db = new DatabaseTable(this);
    handleIntent(getIntent());
}

public void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Intent contactIntent = new Intent(this, ContactActivity.class);
        contactIntent.setData(intent.getData());
        startActivity(contactIntent);
        finish();
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }

}

private void showResults(String query) {
    int name = 0;
    String c = "";
    Cursor cursor = db.getContactMatches(query, null);
    int cursor_length = db.cursorLength(cursor);
    String[] display = new String[cursor_length];
    list = (ListView)findViewById(android.R.id.list);
    int x = 0;

    //Loading data through Cursor into String array
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
    {
        name = cursor.getColumnIndex(DatabaseTable.COL_NAME);
        c = (cursor.getString(name));
        display[x] = c;
        c = "";
        x = x + 1;
        name = 0;
    }

    ArrayAdapter<String> contactAdapter = new ArrayAdapter<String>(this,
              R.layout.activity_search_results, android.R.id.list, display);
    list.setAdapter(contactAdapter);
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent contactIntent = new Intent(getApplicationContext(), ContactActivity.class);
            contactIntent.setData(getIntent().getData());
            startActivity(contactIntent);
        }
    });
}

日志:

12-27 20:57:24.962: E/AndroidRuntime(635): FATAL EXCEPTION: main
12-27 20:57:24.962: E/AndroidRuntime(635): java.lang.NullPointerException
12-27 20:57:24.962: E/AndroidRuntime(635):  at com.example.myfirstapp.DatabaseTable.query(DatabaseTable.java:99)
12-27 20:57:24.962: E/AndroidRuntime(635):  at com.example.myfirstapp.DatabaseTable.getContactMatches(DatabaseTable.java:92)
12-27 20:57:24.962: E/AndroidRuntime(635):  at com.example.myfirstapp.SearchResultsActivity.showResults(SearchResultsActivity.java:51)
12-27 20:57:24.962: E/AndroidRuntime(635):  at com.example.myfirstapp.SearchResultsActivity.handleIntent(SearchResultsActivity.java:43)
4

1 回答 1

0

似乎您从未实际实例化引用变量DBHelper。这意味着虽然它是全局的,但它仍然是null。在某个地方,您将需要实例化它。如果 DbHelper 的构造函数也需要一个Context传递的,在DatabaseTable的构造函数中你可以这样做:

public DataBaseTable (Context context)
{
 DBHelper = new DbHelper (context);
 //rest of your initialization stuff here.
}

否则,就做DBHelper = new DbHelper ();

于 2012-12-28T03:47:08.443 回答