我将实现该方法来检索字符串的记录,但是当涉及到执行时,它会像Log cat所说的那样变成运行时错误。您能否告诉我们如何正确初始化光标?
logcat的
07-08 10:10:34.620: D/result(8036): true
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57c06000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57d6c000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57cbb000 size:466944
07-08 10:10:34.710: D/debug(8036): Order 87318702
07-08 10:10:34.710: D/pcDbHelper(8036): pcDbHelper com.example.recordandmovie.PhotoDbAdapter@412cad30
07-08 10:10:34.841: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.931: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.941: D/debug Cursor(8036): can be created
07-08 10:10:34.941: E/CursorWindow(8036): Failed to read row 0, column 1 from a CursorWindow which has 0 rows, 4 columns.
07-08 10:10:34.941: W/dalvikvm(8036): threadid=1: thread exiting with uncaught exception (group=0x40c051f8)
07-08 10:10:34.951: E/AndroidRuntime(8036): FATAL EXCEPTION: main
07-08 10:10:34.951: E/AndroidRuntime(8036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recordandmovie/com.example.recordandmovie.Main6}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.os.Looper.loop(Looper.java:137)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.main(ActivityThread.java:4519)
07-08 10:10:34.951: E/AndroidRuntime(8036): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
07-08 10:10:34.951: E/AndroidRuntime(8036): at dalvik.system.NativeStart.main(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): 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.
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.CursorWindow.nativeGetString(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.CursorWindow.getString(CursorWindow.java:450)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.example.recordandmovie.Main6.onCreate(Main6.java:39)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.Activity.performCreate(Activity.java:4485)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
以下是我的工作
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
public class Main6 extends Activity {
private PhotoDbAdapter pcDbHelper;
private String orderidGenerated;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
Intent intent = getIntent();
if(intent!=null){
orderidGenerated = intent.getStringExtra("order");
}
Log.d("debug" , "Order " + orderidGenerated);
if(pcDbHelper ==null){
pcDbHelper = new PhotoDbAdapter(this);
pcDbHelper.open();
}
Log.d("pcDbHelper" , "pcDbHelper " + pcDbHelper);
Cursor cur = pcDbHelper.fetchOrderByRef(orderidGenerated);
if(cur!=null){
cur.moveToFirst();
Log.d("debug Cursor" , "can be created");
Log.d("KEY_PHOTO_HKID_HEX" , cur.getString(1));
Log.d("KEY_PHOTO_ADDRESS_HEX" , cur.getString(2));
Log.d("KEY_PHOTO_BANK_HEX" , cur.getString(3));
startManagingCursor(cur);
cur.close();
}else{
Log.d("debug Cursor" , "cannot be created");
}
}
}
public Cursor fetchOrderByRef(String refno) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {
KEY_REFNO ,
KEY_PHOTO_HKID_HEX ,
KEY_PHOTO_ADDRESS_HEX ,
KEY_PHOTO_BANK_HEX
},KEY_REFNO + "=" + refno,
null, null, null, null, null);
return mCursor;
}
答案 0 :(得分:5)
查询时,CursorWindow的每一行都限制为2097152.大小由com.android.internal.R.integer.config_cursorWindowSize设置。你可以在https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/config.xml找到config_cursorWindowSize
答案 1 :(得分:4)
错误消息显示
无法从包含0行,4列的CursorWindow中读取第0行第1列。
所以你的查询返回0行。你的if语句应为:
if(cur!=null){
// Best to manage any non-null cursor as then it will be closed when the
// activity is stopped
startManagingCursor(cur);
// Only if a row was found
if(cur.moveToFirst()) {
Log.d("debug Cursor" , "can be created");
Log.d("KEY_PHOTO_HKID_HEX" , cur.getString(1));
Log.d("KEY_PHOTO_ADDRESS_HEX" , cur.getString(2));
Log.d("KEY_PHOTO_BANK_HEX" , cur.getString(3));
}else{
// Handle no rows returned
}
}else{
Log.d("debug Cursor" , "cannot be created");
}