android应用程序在执行此查询时强制关闭
您好,我正在尝试执行此查询,但每次我在模拟器中运行时,它都会显示强制关闭消息。请有人告诉我查询出了什么问题?
public String searchRecord(String f) {
Cursor cur= db.query(DATABASE_TABLE,new String[] { UserName, Age, Sex,DOB} , UserName +" like 'sa%'" , null ,null ,null ,null , "5");
cur.moveToFirst();
return cur.getString(cur.getColumnIndex(UserName));
}
这是 logcat。
06-30 15:02:47.342: WARN/dalvikvm(300): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): FATAL EXCEPTION: main
06-30 15:02:47.632: ERROR/AndroidRuntime(300): java.lang.ClassCastException: android.text.SpannableStringBuilder
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.menu.san.OptionsmenuActivity$2.onTextChanged(OptionsmenuActivity.java:66)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.handleTextChanged(TextView.java:6172)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.doKeyDown(TextView.java:4304)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.onKeyDown(TextView.java:4149)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.View.dispatchKeyEvent(View.java:3740)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-30 15:02:47.812: WARN/ActivityManager(67): Force finishing activity com.menu.san/.OptionsmenuActivity
06-30 15:02:48.832: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{44f35b60 com.menu.san/.OptionsmenuActivity}
Hi i am trying to execute this query but every time i run in the emulator, it shows force close message. Someone plz tell me whats wrong in the query?
public String searchRecord(String f) {
Cursor cur= db.query(DATABASE_TABLE,new String[] { UserName, Age, Sex,DOB} , UserName +" like 'sa%'" , null ,null ,null ,null , "5");
cur.moveToFirst();
return cur.getString(cur.getColumnIndex(UserName));
}
this is the logcat.
06-30 15:02:47.342: WARN/dalvikvm(300): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): FATAL EXCEPTION: main
06-30 15:02:47.632: ERROR/AndroidRuntime(300): java.lang.ClassCastException: android.text.SpannableStringBuilder
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.menu.san.OptionsmenuActivity$2.onTextChanged(OptionsmenuActivity.java:66)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.handleTextChanged(TextView.java:6172)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.doKeyDown(TextView.java:4304)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.widget.TextView.onKeyDown(TextView.java:4149)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.View.dispatchKeyEvent(View.java:3740)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-30 15:02:47.632: ERROR/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-30 15:02:47.812: WARN/ActivityManager(67): Force finishing activity com.menu.san/.OptionsmenuActivity
06-30 15:02:48.832: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{44f35b60 com.menu.san/.OptionsmenuActivity}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以发布 logcat 吗?
我认为你的问题来自于你的 select 参数的语法
尝试添加 \ 像这样:
USER_LOGIN + " LIKE \"" + 登录 +"\""
can you post logcat ?
I think your problem comes the syntaxe of your select argument
try to add the \ like this :
USER_LOGIN + " LIKE \"" + login +"\""
尝试发布您的 logcat 输出,我们可能可以提供进一步的帮助,但乍一看,我可以看到一些潜在的问题:
getColumnIndex()
将返回 -1,这将导致cur.getString()
出现问题cur.getString
和cur.getColumnIndex
都会失败I' 将代码更新为如下所示:
d 建议您 可能需要考虑使用
getColumnIndexOrThrow
而不是getColumnIndex
,因为它会引发异常,并使与缺失列相关的错误更加明显。另一个问题是您确定 UserName、Age、Sex 和 DOB 是变量,还是实际的列名称?如果它们是文字列名称,您应该使用以下查询:
如果是这样,您还应该将
getColumnIndex
调用更改为:Try posting your logcat output and we can probably help further, but on first glance I can see a couple of potential problems:
getColumnIndex()
will return -1, which will cause problems withcur.getString()
cur.getString
andcur.getColumnIndex
will failI'd suggest you update your code to something like this:
You may want to consider using
getColumnIndexOrThrow
instead ofgetColumnIndex
as it will throw an exception and will make errors relating to missing columns more obvious.Another question is are you sure that UserName, Age, Sex, and DOB are variables, or are they the actual column name? If they are the literal column name, you should use the following query instead:
If so, you should also change your
getColumnIndex
call to:我建议你做这样的事情,它对我来说效果很好:
然后用光标中检索到的数据做任何你想做的事情
I recommend you to do something like this,it works fine for me:
Then do anything you waht with the retrieved data in the Cursor