2

亲爱的stackoverflowers,

我的 android 应用程序中有一个数据库,我向它发送一个字符串并返回一个光标。这是代码:

public Cursor listSearch(String phonename) {
        Cursor mCursor = mDb.query(SQLITE_AllPhones, new String[] { KEY_ROWID,
                KEY_OEM, KEY_PHONENAME, KEY_MODELNUMBER, KEY_AVAILABILITYDATE,
                KEY_URL, KEY_URL2, KEY_URL3, KEY_RELEASEDATE, KEY_SIM,
                KEY_ANDROID, KEY_COLORS, KEY_DIMENSIONS, KEY_WEIGHT,
                KEY_DISPLAYTYPE, KEY_DISPLAYSIZE, KEY_RESOLUTION, KEY_PPI,
                KEY_CARDSLOT, KEY_INTERNAL, KEY_CAMERA, KEY_CAMERAFLASH,
                KEY_FEATURES, KEY_VIDEO, KEY_FRONTCAMERA, KEY_CHIPSET, KEY_CPU,
                KEY_GPU, KEY_MEMORY, KEY_BATTERY }, null, null, null, null,
                null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }

        String cursor = "Select * FROM AllPhonesTable WHERE phone_name = '"
                + phonename + "'";
        Log.e("cursor", cursor);
        mCursor = mDb.rawQuery(cursor, null);

        return mCursor;

    }

然后,我从另一个类访问该游标:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        gotBasket = BigSpecs.this.getIntent().getExtras();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.specs);
        imageLoader = new com.androidarabia.lazylist.ImageLoader2(
                getApplicationContext());
        apd = new AllPhonesDatabase(this.getApplicationContext()).open();
        c = apd.listSearch(gotBasket.getString("phonename"));
        Log.d("cursor", "The Cursor Number" + c.getCount());

        initialize();
        setuptext();
    }

然后我根据光标做我的工作:

private void setuptext() {
        // TODO Auto-generated method stub
        final String imageurl = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_URL));
        imageLoader.DisplayImage(imageurl, pic1);

        final String Releasedate = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_RELEASEDATE));
        releasedate.setText("" + Releasedate);
        final String Androidversion = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_ANDROID));
        androidversion.setText("" + Androidversion);
        final String Sim = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_SIM));
        tvsim.setText("" + Sim);
        final String Dimensions = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_DIMENSIONS));
        dimensions.setText("" + Dimensions);
        final String Weight = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_WEIGHT));
        weight.setText("" + Weight);
        final String Color = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_COLORS));
        color.setText("" + Color);
        final String Dt = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_DISPLAYTYPE));
        displaytype.setText("" + Dt);
        final String ds = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_DISPLAYSIZE));
        displaysize.setText("" + ds);
        final String dr = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_RESOLUTION));
        displayresolution.setText("" + dr);
        final String PPI = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_PPI));
        ppi.setText("" + PPI);
        final String cardSlot = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_CARDSLOT));
        cardslot.setText("" + cardSlot);
        final String INTERNAL = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_INTERNAL));
        internal.setText("" + INTERNAL);
        final String CR = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_CAMERA));
        cameraresolution.setText("" + CR);
        final String FLASH = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_CAMERAFLASH));
        flash.setText("" + FLASH);
        final String FEATURES = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_FEATURES));
        camerafeatures.setText("" + FEATURES);
        final String VIDEO = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_VIDEO));
        video.setText("" + VIDEO);
        final String FFC = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_FRONTCAMERA));
        frontfacingcamera.setText("" + FFC);
        final String CHIPSET = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_CHIPSET));
        chipset.setText("" + CHIPSET);
        final String CPU = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_CPU));
        cpu.setText("" + CPU);
        final String GPU = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_GPU));
        gpu.setText("" + GPU);
        final String Memory = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_MEMORY));
        ram.setText("" + Memory);
        final String BATTERY = c.getString(c
                .getColumnIndex(AllPhonesDatabase.KEY_BATTERY));
        battery.setText("" + BATTERY);

    }

只是一些从数据库中获取文本并设置它的简单代码。但我得到这个错误!

08-19 19:18:17.591: E/AndroidRuntime(7902): FATAL EXCEPTION: main
08-19 19:18:17.591: E/AndroidRuntime(7902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidarabia.phones/com.androidarabia.big.BigSpecs}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread.access$700(ActivityThread.java:151)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.os.Looper.loop(Looper.java:137)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread.main(ActivityThread.java:5293)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at java.lang.reflect.Method.invoke(Method.java:511)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at dalvik.system.NativeStart.main(Native Method)
08-19 19:18:17.591: E/AndroidRuntime(7902): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at com.androidarabia.big.BigSpecs.setuptext(BigSpecs.java:62)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at com.androidarabia.big.BigSpecs.onCreate(BigSpecs.java:50)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.Activity.performCreate(Activity.java:5250)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-19 19:18:17.591: E/AndroidRuntime(7902):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
08-19 19:18:17.591: E/AndroidRuntime(7902):     ... 11 more

谢谢,您的帮助将不胜感激=)!

4

3 回答 3

1

您需要在查询后调用 mCursor.moveToFirst() 方法。检查您是否在此行附近查询后执行此操作 checkPosition(AbstractCursor.java:424)。

于 2013-08-19T17:07:30.073 回答
0

您必须将光标移动到第一行才能从中读取。您对第一个查询做了一些处理,但对第二个查询没有这样做(例如,它在 之后丢失mCursor = mDb.rawQuery(cursor, null);)。

final Cursor cursor = /*...*/;
if (cursor.getCount() < 1){
  // handle empty cursors somehow, e.g. no results
  return;
}
cursor.moveToNext();
// or
cursor.moveToFirst();
于 2013-08-19T16:28:08.193 回答
0

在从中访问数据之前检查游标

If(cursor!=null && cursor.moveToFirst())
于 2013-08-19T16:33:22.257 回答