0

我将实现检索字符串记录的方法,但是当涉及到执行时,它会变成运行时错误,如 Log cat 所述。您能告诉我们如何正确初始化光标吗?

日志猫

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;

}
4

2 回答 2

5

查询时,CursorWindow 的每一行限制为 2097152。大小由 com.android.internal.R.integer.config_cursorWindowSize 设置。您可以在https://github.com/android/platform_frameworks_base/blob/找到 config_cursorWindowSize主/核心/res/res/values/config.xml

于 2013-12-19T07:00:42.440 回答
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");
}
于 2013-07-08T02:29:28.403 回答