如何从mainclass获取返回字符串

时间:2014-11-25 08:02:25

标签: java android android-sqlite

DbHelper.java

public static String returntitle(Context context , String query) {
    DbHelper dbhelper = new DbHelper(context);
    SQLiteDatabase db = dbhelper.getReadableDatabase();
    // Cursor cursor = null;
    String temp_title = null;
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        do {
            temp_title = (cursor.getString(1));
        } while (cursor.moveToNext());
    }

    return temp_title;
}

如何获取mainclass.java中的temp_title

mainClass.java

private void gettitleform_db() {
    // TODO Auto-generated method stub
    String str = "SELECT cl_title_tb_resume FROM tb_resume WHERE _id = 2 ";
    String temp2 = DbHelper.returntitle(this, str);
    System.out.println(temp2);
}

上面的代码返回一个异常。我的代码有什么问题。提前致谢

logcat的

11-25 13:08:52.330: W/ApplicationPackageManager(17274): getCSCPackageItemText()
11-25 13:08:52.390: D/mali_winsys(17274): new_window_surface returns 0x3000
11-25 13:08:58.145: W/ApplicationPackageManager(17274): getCSCPackageItemText()
11-25 13:08:58.160: D/AbsListView(17274): Get MotionRecognitionManager
11-25 13:08:58.175: E/CursorWindow(17274): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
11-25 13:08:58.175: D/AndroidRuntime(17274): Shutting down VM
11-25 13:08:58.175: W/dalvikvm(17274): threadid=1: thread exiting with uncaught exception (group=0x41770c08)
11-25 13:08:58.175: E/AndroidRuntime(17274): FATAL EXCEPTION: main
11-25 13:08:58.175: E/AndroidRuntime(17274): Process: com.absind.resumebuilder, PID: 17274
11-25 13:08:58.175: E/AndroidRuntime(17274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.absind.resumebuilder/com.absind.resumebuilder.AddList}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread.access$800(ActivityThread.java:157)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.os.Looper.loop(Looper.java:157)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread.main(ActivityThread.java:5293)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at java.lang.reflect.Method.invoke(Method.java:515)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1259)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at dalvik.system.NativeStart.main(Native Method)
11-25 13:08:58.175: E/AndroidRuntime(17274): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.database.CursorWindow.nativeGetString(Native Method)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.database.CursorWindow.getString(CursorWindow.java:438)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at com.absind.resumebuilder.DbHelper.returntitle(DbHelper.java:53)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at com.absind.resumebuilder.AddList.gettitleform_db(AddList.java:123)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at com.absind.resumebuilder.AddList.onCreate(AddList.java:42)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.Activity.performCreate(Activity.java:5389)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-25 13:08:58.175: E/AndroidRuntime(17274):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
11-25 13:08:58.175: E/AndroidRuntime(17274):    ... 11 more

3 个答案:

答案 0 :(得分:1)

无法从CursorWindow读取第0行第1列。在从中访问数据之前,确保Cursor已正确初始化

答案 1 :(得分:1)

为什么你确定列索引是1? 试试这个:

temp_title = cursor.getString(cursor.getColumnIndex("cl_title_tb_resume"));

答案 2 :(得分:1)

CursorWindow有1行1列。并且您正在访问不存在的第1列。将1改为0.

temp_title = (cursor.getString(0));