Android SQLit 搜索:NullPointerException?
我需要对 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 SearchActivity
for 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 NullPointerException 的描述可能会提供更多信息。但 NullPointerException 很可能发生在此处:
或此处:
在第 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:
or here:
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 ifcursor
isnull
. Another thing could be that you never initializemyDbHelper
.