Android SQLit 搜索:NullPointerException?

发布于 2024-11-24 18:02:18 字数 1417 浏览 1 评论 0原文

我需要对 SQLite 数据库执行搜索操作。

在我的活动中,我有用于输入关键字的 EditText 和搜索Button。用户单击按钮后,EditText 文本将存储在 keyword 中,并发送到 DBHelper 类中的 search(String keywords) 方法。

我的 SearchActivity 中有以下代码用于 onCreate()onClick()

//SearchActivity class extends ListActivity and implements OnClickListener 
Button search;
DataBaseHelper myDbHelper;
SimpleCursorAdapter ca;
ListView lv;
EditText txt;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_layout);

    txt = (EditText) findViewById(R.id.search_term);
    search = (Button) findViewById(R.id.search_button);

    search.setOnClickListener(this);
}

public void onClick(View v) {
    try {
        myDbHelper.openDataBase();
    } catch (SQLException sqle) {}

    try {
    String keyword= txt.getText().toString();
    Cursor cursor = myDbHelper.search(keyword); //A method in my DB
    cursor.moveToFirst();
    String[] columns = {cursor.getColumnName(1)}; 
    int[] columnsLayouts = {R.id.item_title};
    ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
    lv = getListView();
    lv.setAdapter(ca);

    } catch(Exception e){}
}

但是,我得到了 NullPointerException日志猫!!我找不到错误在哪里。

任何人都可以帮忙吗?

I need to perform a search operation into my SQLite DB.

In my Activity, I have EditText for entering the keyword and a search Button. Once the user clicks the button, the EditText text will be stored in the keyword and sent to a search(String keyword) method in the DBHelper class.

I have this code in my SearchActivityfor onCreate() and onClick():

//SearchActivity class extends ListActivity and implements OnClickListener 
Button search;
DataBaseHelper myDbHelper;
SimpleCursorAdapter ca;
ListView lv;
EditText txt;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_layout);

    txt = (EditText) findViewById(R.id.search_term);
    search = (Button) findViewById(R.id.search_button);

    search.setOnClickListener(this);
}

public void onClick(View v) {
    try {
        myDbHelper.openDataBase();
    } catch (SQLException sqle) {}

    try {
    String keyword= txt.getText().toString();
    Cursor cursor = myDbHelper.search(keyword); //A method in my DB
    cursor.moveToFirst();
    String[] columns = {cursor.getColumnName(1)}; 
    int[] columnsLayouts = {R.id.item_title};
    ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
    lv = getListView();
    lv.setAdapter(ca);

    } catch(Exception e){}
}

However, I got NullPointerException in the LogCat!! I couldn't find where the error is.

Anyone can help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-12-01 18:02:18

对 NullPointerException 的描述可能会提供更多信息。但 NullPointerException 很可能发生在此处:

1: Cursor cursor = myDbHelper.search(keyword); //A method in my DB
2: cursor.moveToFirst();
3: String[] columns = {cursor.getColumnName(1)}; 

或此处:

1: try {
2:    myDbHelper.openDataBase();
3: } catch (SQLException sqle) {}

在第 1 行的第一部分:或第 2 行:或在第 2 行的第二部分:

您应该始终检查依赖于数据存在的对象是否 <代码>不为空。因此,您应该检查 cursor 是否为 null。另一件事可能是您从未初始化myDbHelper

The description to the NullPointerException could be a little more informative. But it is very likely that the NullPointerException occurs here:

1: Cursor cursor = myDbHelper.search(keyword); //A method in my DB
2: cursor.moveToFirst();
3: String[] columns = {cursor.getColumnName(1)}; 

or here:

1: try {
2:    myDbHelper.openDataBase();
3: } catch (SQLException sqle) {}

Either in the first part in line 1: or 2: or in the second part in line 2:

You should always check if objects which rely on the existence of data are not null. So you should check if cursor is null. Another thing could be that you never initialize myDbHelper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文