android应用程序在执行此查询时强制关闭

发布于 2024-11-17 18:09:16 字数 5134 浏览 0 评论 0原文

您好,我正在尝试执行此查询,但每次我在模拟器中运行时,它都会显示强制关闭消息。请有人告诉我查询出了什么问题?

 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 技术交流群。

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

发布评论

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

评论(3

初见你 2024-11-24 18:09:16

你可以发布 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 +"\""

新一帅帅 2024-11-24 18:09:16

尝试发布您的 logcat 输出,我们可能可以提供进一步的帮助,但乍一看,我可以看到一些潜在的问题:

  • 如果 UsernName 列不存在,getColumnIndex() 将返回 -1,这将导致 cur.getString() 出现问题
  • 如果您的查询未返回任何结果,则 cur.getStringcur.getColumnIndex 都会失败

I' 将代码更新为如下所示:

public String searchRecord(String f) {
    String user;
    Cursor cur= db.query(DATABASE_TABLE,new String[] { UserName, Age, Sex,DOB} , UserName +" like 'sa%'" , null ,null ,null ,null , "5");
    if ( cur.getCount() > 0 ) { // Only attempt to get the UserName if we actually got some results
        cur.moveToFirst();
        user = cur.getString ( cur.getColumnIndex ( UserName ) );
    }
    else { // If no results returned by the query, return a NULL string;
        user = null;
    }
    return user;
}

d 建议您 可能需要考虑使用 getColumnIndexOrThrow 而不是 getColumnIndex,因为它会引发异常,并使与缺失列相关的错误更加明显。

另一个问题是您确定 UserName、Age、Sex 和 DOB 是变量,还是实际的列名称?如果它们是文字列名称,您应该使用以下查询:

Cursor cur= db.query(DATABASE_TABLE,new String[] { "UserName", "Age", "Sex", "DOB" } , "UserName like 'sa%'" , null ,null ,null ,null , "5");

如果是这样,您还应该将 getColumnIndex 调用更改为:

cur.getColumnIndex ( "UserName" )

Try posting your logcat output and we can probably help further, but on first glance I can see a couple of potential problems:

  • If the UsernName column does not exist, getColumnIndex() will return -1, which will cause problems with cur.getString()
  • If your query does not return any results, both cur.getString and cur.getColumnIndex will fail

I'd suggest you update your code to something like this:

public String searchRecord(String f) {
    String user;
    Cursor cur= db.query(DATABASE_TABLE,new String[] { UserName, Age, Sex,DOB} , UserName +" like 'sa%'" , null ,null ,null ,null , "5");
    if ( cur.getCount() > 0 ) { // Only attempt to get the UserName if we actually got some results
        cur.moveToFirst();
        user = cur.getString ( cur.getColumnIndex ( UserName ) );
    }
    else { // If no results returned by the query, return a NULL string;
        user = null;
    }
    return user;
}

You may want to consider using getColumnIndexOrThrow instead of getColumnIndex 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:

Cursor cur= db.query(DATABASE_TABLE,new String[] { "UserName", "Age", "Sex", "DOB" } , "UserName like 'sa%'" , null ,null ,null ,null , "5");

If so, you should also change your getColumnIndex call to:

cur.getColumnIndex ( "UserName" )
断舍离 2024-11-24 18:09:16

我建议你做这样的事情,它对我来说效果很好:

public Cursor SearchRecord(String search) {        
    Cursor c;        
    String[]columns = {UserName, Age, Sex,DOB};
    String Limit = "0,5";

    String query = search != null ? UserName + " LIKE '" + search + "%' " : "";

    c = myDataBase.query(DATABASE_TABLE,columns, query, null, null, null, null, Limit);

    return c;
}

然后用光标中检索到的数据做任何你想做的事情

I recommend you to do something like this,it works fine for me:

public Cursor SearchRecord(String search) {        
    Cursor c;        
    String[]columns = {UserName, Age, Sex,DOB};
    String Limit = "0,5";

    String query = search != null ? UserName + " LIKE '" + search + "%' " : "";

    c = myDataBase.query(DATABASE_TABLE,columns, query, null, null, null, null, Limit);

    return c;
}

Then do anything you waht with the retrieved data in the Cursor

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