开发者

android application force close on executing this query

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/Androi开发者_运维知识库dRuntime(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}


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 +"\""


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" )


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜