本文介绍了由于游标查询错误,应用程序停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 点击getDetails按钮后,我想使用列中的子字符串获取详细信息。 我的代码中没有错误,但我觉得db.query应该有一些错误。 有人可以解决它吗? public void getDetails(View v) { String s1 = name .getText()的toString(); String sub1 = s1.substring( 0 ,s1.indexOf(s1)); String s3 = vivzHelper.getData(sub1); Message.message( this ,s3); } public String getData(String name){ SQLiteDatabase db = helper.getWritableDatabase(); String [] columns = {VivzHelper.NAME}; String [] selectionArgs = {name}; Cursor cursor = null; cursor = db.query(VivzHelper.NAME,columns, VivzHelper.NAME +=?,null,null,null,null,null); StringBuffer buffer = new StringBuffer(); while(cursor.moveToNext( )) { int indexID = cursor.getColumnIndex(VivzHelper.UID); int nameID = cursor.getInt(indexID); int accountNameIDX = cursor.getColumnIndex(VivzHelper.NAME); String accountName = cursor.getString((accountNameIDX)); buffer.append (nameID ++ accountName +/ n); } return buffer.toString(); } 解决方案 在你返回之前尝试关闭光标 cusror.close(); On click of 'getDetails' button I want to get details using substring from a column.There is no error in my code but I feel there should be some wrong in the db.query.Can some one sort it out?public void getDetails(View v){ String s1=name.getText().toString(); String sub1=s1.substring(0,s1.indexOf(s1)); String s3= vivzHelper.getData(sub1); Message.message(this,s3);} public String getData(String name) { SQLiteDatabase db = helper.getWritableDatabase(); String[] columns = {VivzHelper.NAME}; String[] selectionArgs = {name}; Cursor cursor= null; cursor = db.query(VivzHelper.NAME, columns, VivzHelper.NAME + "=?", null, null, null, null, null); StringBuffer buffer = new StringBuffer(); while(cursor.moveToNext()) { int indexID=cursor.getColumnIndex(VivzHelper.UID); int nameID=cursor.getInt(indexID); int accountNameIDX=cursor.getColumnIndex(VivzHelper.NAME); String accountName=cursor.getString((accountNameIDX)); buffer.append(nameID+" "+ accountName+"/n"); } return buffer.toString(); } 解决方案 try closing the cursor before you returncusror.close(); 这篇关于由于游标查询错误,应用程序停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 01:54