【问题标题】:Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 2 columns无法从具有 2 行 2 列的 CursorWindow 读取第 1 行第 -1 列
【发布时间】:2016-03-30 08:31:36
【问题描述】:

我是 Android 开发的初学者,我正在尝试将数据从光标加载到列表视图。但是,我收到此错误

12-23 22:51:12.711 16696-16696/com.chaos.todolist E/CursorWindow: 无法从有 2 行的 CursorWindow 读取第 1 行第 -1 列, 2 列。

这是我的表演课

package com.chaos.todolist;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class show extends AppCompatActivity {
    private  ListView list;
    private  DataBase db;
    private ArrayAdapter<String> adapter;
    private  Cursor result;
    private  String[] names;
    int[] ids;
    taskbody t;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        list=(ListView) findViewById(R.id.list);
        db=new DataBase(this);
         Log.d("show", "is going to load database");
     //   list.setOnItemClickListener((AdapterView.OnItemClickListener) this);
    }

    protected void onResume() {
        super.onResume();
        db.open();
        LoadDataFromDataBaseToListView();
    }
    protected  void onPause()
    {
        super.onPause();
    }

    private void LoadDataFromDataBaseToListView() {
        db.open();
        result=db.getAllTasks();
        if(result!=null) {
            int i = 0;
            names = new String[result.getCount()];
            ids = new int[result.getCount()];
            try {

                if (result.moveToFirst()) {
                    int index_id = result.getColumnIndex(t.COLUMN_ID);
                    int index_name = result.getColumnIndex(t.COLUMN_NAME);
                    while (result.moveToNext()) {
                        ids[i] = result.getInt(index_id);
                        names[i] = result.getString(index_name);
                        i++;
                    }
                    db.close();
                    adapter = new ArrayAdapter<String>(
                            getApplicationContext(),
                            R.layout.taskitem, R.id.name,
                            names);
                    list.setAdapter(adapter);
                }
            }catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
        else {
            Toast.makeText(this, "is null", Toast.LENGTH_LONG).show();
            adapter = new ArrayAdapter<String>(
                    getApplicationContext(),
                    android.R.layout.simple_list_item_1);

            list.setAdapter(adapter);
        }
    }
 //  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

   //   ViewTask(position);

    //}
    /*private void ViewTask(int position) {
        try {
            Intent intent = new Intent(show.this, ViewTask.class);
            intent.putExtra("_ID", ids[position]);
            Toast.makeText(getApplicationContext(), ids[position] + "",
                    Toast.LENGTH_LONG).show();
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }*/

}

这是包含获取所有任务方法的类

package com.chaos.todolist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataBase {

    private SQLiteDatabase mDatabase;
    private DB_Helper TODOHelper;

    public DataBase(Context context) {
        TODOHelper = new DB_Helper(context);
        open();
    }

    protected void open()
    {
        mDatabase = TODOHelper.getWritableDatabase();
    }

    public void close() {
        if (mDatabase != null) {
            mDatabase.close();
        }
    }

    public Cursor getAllTasks()
    {
        open();
        String[] col = { taskbody.COLUMN_ID, taskbody.COLUMN_NAME };
        Cursor cursor = mDatabase.query(
                taskbody.TABLE_TASKS, // table name
                col , // column names
                null, // where clause
                null, // where params
                null, // groupby
                null, // having
                taskbody.COLUMN_NAME // orderby
        );

        return cursor;
    }

    public Cursor getTaskById(int id) {
        String[] columns = { taskbody.COLUMN_NAME, taskbody.COLUMN_DETAILS,
                taskbody.COLUMN_ADDRESS};

        return mDatabase.query(
                taskbody.TABLE_TASKS, // table name
                columns, // column names
                taskbody.COLUMN_ID + " = " + id, // where clause // id param. could be here or appended as it is ^
                null, // where params
                null, // groupby
                null, // having
                null // orderby
        );
    }

    public void deleteTaskById(int id) {
        open();
        mDatabase.delete(
                taskbody.TABLE_TASKS, // table name
                taskbody.COLUMN_ID +"="+ id, // where clause
                null // where params
        );
        close();
    }

    public void insertTask(String name,String details,String address) {
        open();
        mDatabase.beginTransaction();
         try {
             ContentValues newTask = new ContentValues();
             newTask.put(taskbody.COLUMN_NAME, name);
             newTask.put(taskbody.COLUMN_DETAILS, details);
             newTask.put(taskbody.COLUMN_ADDRESS, address);
             mDatabase.insert(taskbody.TABLE_TASKS, null, newTask);
         } finally {
             mDatabase.endTransaction();
         }
        mDatabase.close();

        }


    public void updateTask(int id,String name,String details,String address) {
        open();
        ContentValues editTask = new ContentValues();
        editTask.put(taskbody.COLUMN_NAME, name);
        editTask.put(taskbody.COLUMN_DETAILS, details);
        editTask.put(taskbody.COLUMN_ADDRESS, address);
        mDatabase.update(
                taskbody.TABLE_TASKS, // table name
                editTask, // values
                taskbody.COLUMN_ID + " = " + id, // where clause
                null // where params
        );
        mDatabase.close();
    }
}

【问题讨论】:

  • 哪一行给你错误?
  • 您的结果似乎有问题。

标签: java android mysql sqlite


【解决方案1】:

这个错误Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 2 columns 意味着两个东西:

  1. 该列不存在。
  2. 列名不正确。

【讨论】:

  • 我删除了数据库然后它被创建了问题解决了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 2013-06-16
相关资源
最近更新 更多