【问题标题】:Android cursor problems "Make sure the Cursor is initialized correctly before accessing data from it."Android 游标问题“确保游标在访问数据之前已正确初始化。”
【发布时间】:2018-05-22 15:54:06
【问题描述】:

java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行 col -1。确保 Cursor 在从中访问数据之前已正确初始化。

我想从 SQL 数据库中获取照片名称,然后从可绘制文件中获取照片并将照片链接到 ListView 内的 ImageView 项目 以下代码有什么问题,请帮我解决,谢谢

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class HotProduct extends ListActivity {

    private ProductSQLiteHelper sqlHelper;
    private SQLiteDatabase infodb;
    private String[] columnsToSelect;
    private String[] columnsToSelect2;
    private Cursor infoCursor;
    private SimpleCursorAdapter dbAdapter;
    private SimpleCursorAdapter dbAdapter2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotproduct_middle_page);
        columnsToSelect = new String[] {
                ProductSQLiteHelper.PRODUCT_ID,
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE


        };




        Resources res = getResources();
        Log.v("Res:",String.valueOf(res));


//        setTitle("Hot Product");



        sqlHelper = new ProductSQLiteHelper(this);

        infodb = sqlHelper.getReadableDatabase();


        Cursor infoCursor= infodb.rawQuery("SELECT photo FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        infoCursor.moveToFirst();
        if (!infoCursor.isAfterLast()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

        String columnsToDisplay[] = {
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE
//                ProductSQLiteHelper.PRODUCT_PHOTO
        };


        int mappingToView[] = {
                R.id.productName,
                R.id.productBrand,
                R.id.productPrice
//                R.id.productPhoto
        };





        String args[] = {"1"};
        infoCursor = infodb.query(ProductSQLiteHelper.TABLE_NAME,columnsToSelect," hot = ? ",args,null,null,null);

        String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex("photo"));
        Log.v("resource name",posterResourceName);
        dbAdapter = new SimpleCursorAdapter(this,R.layout.hotproduct_middle_page_row,infoCursor,columnsToDisplay,mappingToView,0);


        setListAdapter(dbAdapter);



        }




}

我发现问题出现在

      Cursor infoCursor= infodb.rawQuery("SELECT photo FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        infoCursor.moveToFirst();
        if (!infoCursor.isAfterLast()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

【问题讨论】:

标签: java android sqlite listview


【解决方案1】:

您的问题是由于 getColumnIndex 没有找到指定的列并因此返回 -1(当该列不是列时它会返回)。根本原因是您正在创建一个带有单列的游标,但在您的代码中您试图访问 2 列,即

  • infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT)))
  • infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO))

光标将只包含 1 列,即照片列。

我建议将 SELECT 子句更改为 "SELECT * FROM phone",这将创建一个包含表中所有列的光标。

但是,代码还有一个缺陷是您没有检查moveToFirst 的结果。

如果光标为空,则 moveToFirst 将返回 false。由于光标不会在最后一次之后,因此从光标检索数据的任何尝试都将失败,因为没有数据。

我建议将您的代码修改为:-

        Cursor infoCursor= infodb.rawQuery("SELECT * FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        while (infoCursor.moveToNext()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

您可能必须编写代码来处理空光标。

【讨论】:

  • 我刚刚试过你的代码......我发现问题仍然存在,但顺便感谢你的回答
  • 如果您编辑问题并从日志中添加堆栈跟踪,则可以分析问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2018-02-27
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
相关资源
最近更新 更多