SQLite不断从CursorWindow返回错误(无法读取行)

时间:2015-05-15 05:10:53

标签: android sqlite multiple-columns

我一直在努力解决这个问题,我找不到解决办法。我一直收到以下错误:

05-15 02:02:39.685    8757-8757/com.financeiro.coolkey.financeiro_2 E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 4 rows, 7 columns.
05-15 02:02:39.686    8757-8757/com.financeiro.coolkey.financeiro_2 D/AndroidRuntime﹕ Shutting down VM
05-15 02:02:39.687    8757-8757/com.financeiro.coolkey.financeiro_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.financeiro.coolkey.financeiro_2, PID: 8757
    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
            at android.database.CursorWindow.nativeGetString(Native Method)
            at android.database.CursorWindow.getString(CursorWindow.java:438)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
            at dao.CalculosDAO.criarCalculadora(CalculosDAO.java:46)
            at dao.CalculosDAO.exibirProdutosPorId(CalculosDAO.java:170)
            at tabs.DetalhesClienteCalculos.onActivityCreated(DetalhesClienteCalculos.java:65)
            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1512)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
            at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
            at com.financeiro.coolkey.view.DetalhesClienteTabbed.onTabSelected(DetalhesClienteTabbed.java:78)
            at android.support.v7.app.ActionBarImplICS$TabWrapper.onTabSelected(ActionBarImplICS.java:558)
            at com.android.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:624)
            at com.android.internal.app.WindowDecorActionBar$TabImpl.select(WindowDecorActionBar.java:1200)
            at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:546)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我不知道发生了什么事!我重新安装了应用程序,检查了数据库,查看了stackoverflow上的所有问题,我仍然找不到解决方案。请帮忙!更多分析代码:

CalculosDAO.java

package dao;

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

import java.util.ArrayList;
import java.util.List;

import model.Calculos;

/**
 * Created by Diego on 24/04/2015.
 */
public class CalculosDAO
{
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase database;

    public CalculosDAO(Context context)
    {
        //abre o banco de dados, evita o erro NullPointerExpection.
        databaseHelper = new DatabaseHelper(context);
        database = databaseHelper.getWritableDatabase();
    }

    private SQLiteDatabase getDatabase()
    {
        if (database == null)
            database = databaseHelper.getWritableDatabase();

        return database;
    }

    //Fecha o banco de dados.
    public void fechar()
    {
        databaseHelper.close();
        database = null;
    }

    private Calculos criarCalculadora(Cursor cursor)
    {
        Calculos model = new Calculos
                (
                        cursor.getInt(cursor.getColumnIndex(DatabaseHelper.Calculos._ID)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.ID_CLIENTE)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.ID_PRODUTO)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.EQUACAO)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.RESULTADO)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.OBS)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.TIPO)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHelper.Calculos.DT_CRIACAO))
                );
        return model;
    }


    public List<Calculos> listarCalculadora()
    {
        Cursor cursor = getDatabase().query(DatabaseHelper.Calculos.TABELA,
                DatabaseHelper.Calculos.COLUNAS, null, null, null, null, null);

        List<Calculos> calculos = new ArrayList<Calculos>();
        while(cursor.moveToNext())
        {
            Log.d("CHECK__ME_LISTAR", "CH: "+calculos +"\\"+cursor);
            Calculos model = criarCalculadora(cursor);
            calculos.add(model);
        }

        cursor.close();
        return calculos;
    }

    /* extra code */

    //procua todos os IDS
    public List<Calculos> exibirProdutosPorId(int id)
    {
        Cursor cursor = getDatabase().query(DatabaseHelper.Calculos.TABELA,
                DatabaseHelper.Calculos.COLUNAS, "id_produto = ?",
                new String[]{ Integer.toString(id) }, null, null, null);

        List<Calculos> calculos = new ArrayList<Calculos>();

        while(cursor.moveToNext())
        {
            Log.d("CHECK__ME_EXIBIRPROD", "CHECKING: "+calculos);
            Calculos model = criarCalculadora(cursor);
            calculos.add(model);
        }



        cursor.close();
        return calculos;
    }
}

DetalhesClienteCalculos.java

package tabs;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.financeiro.coolkey.view.DetalhesClienteTabbed;
import com.financeiro.coolkey.view.R;

import java.util.ArrayList;
import java.util.List;

import adapter.DetalhesClientesCalculosAdapter;
import dao.CalculosDAO;
import dao.ProdutoDAO;
import model.Calculos;
import model.Produto;

/**
 * Created by Diego on 30/04/2015.
 */
public class DetalhesClienteCalculos extends ListFragment {

    private ProdutoDAO produtoDAO;
    private ListView lista;
    //private DetalhesClienteCalculosAdapter dccAdapter;
    private DetalhesClientesCalculosAdapter dccAdapter;
    private List<Produto> produtoItems;

    private CalculosDAO calculosDAO;
    private List<Calculos> calculosItems;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        return inflater.inflate(R.layout.activity_detalhes_cliente_calculos, null, false);
        //View rootView = inflater.inflate(R.layout.activity_detalhes_cliente_calculos, null, false);
        //
        //return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        Context context = getActivity();
        calculosDAO = new CalculosDAO(context);

        calculosItems = new ArrayList<Calculos>();
        calculosItems = calculosDAO.exibirProdutosPorId(DetalhesClienteTabbed.CLIENTE_ID);

        dccAdapter = new DetalhesClientesCalculosAdapter(getActivity(),calculosItems);
        setListAdapter(dccAdapter);

    }
}

DatabaseHelper.java

db.execSQL("CREATE TABLE calculos (_id integer primary key autoincrement, "
        + "id_cliente text, "
        + "id_produto text, "
        + "equacao text, "
        + "resultado text, "
        + "obs text, "
        + "tipo text, "
        + "dt_criacao text not null"
        + ")");

public static class Calculos
{
    public static final String  TABELA = "calculos";

public static final String _ID = "_id";

public static final String ID_CLIENTE = "id_cliente";
public static final String ID_PRODUTO = "id_produto";
public static final String EQUACAO = "equacao";
public static final String RESULTADO = "resultado";
public static final String OBS = "obs";
public static final String TIPO = "tipo";
public static final String DT_CRIACAO = "dt_criacao";

public static final String[] COLUNAS =
        {
                _ID,
                ID_CLIENTE,
                ID_PRODUTO,
                EQUACAO,
                RESULTADO,
                OBS,
                DT_CRIACAO,
        };
}

非常感谢:)

1 个答案:

答案 0 :(得分:2)

您正在少读一列,然后尝试从光标中获取值。

public static final String[] COLUNAS =
    {
            _ID,
            ID_CLIENTE,
            ID_PRODUTO,
            EQUACAO,
            RESULTADO,
            OBS,
            DT_CRIACAO,
    };

在上面的数组列public static final String TIPO = "tipo";中缺失。将它添加到数组然后这应该工作。