2014-11-25 59 views
0

DbHelper.java如何從mainclass返回字符串

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; 
} 

如何獲得TEMP_TITLE在mainclass.java

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 
+2

有什麼異常 – 2014-11-25 08:04:01

+0

發表您的logcat – 2014-11-25 08:04:14

+0

也是爲什麼你正在經歷的所有的遊標的結果嗎?如果你只對一個沒有'do while'循環感興趣,你可以總是取得最後的結果。 – 2014-11-25 08:05:31

回答

1

無法從CursorWindow逐行讀取0,第1欄。確保光標在從其訪問數據前正確初始化

1

爲什麼你確定列索引是1? 試試這個:

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

CursorWindow有1行1列。並且您正在訪問不存在的列號1。變化1變成0

temp_title = (cursor.getString(0));