无法从CursorWindow错误中读取第0行,col -1

时间:2014-08-14 22:06:03

标签: java android sql database

之前我曾使用过SQLDB,但只保存了3个项目。现在我想添加更多,我得到这个错误,我无法解决它。我无法理解我的Cursor在哪里变坏并且不再起作用了。任何人都可以解释我为什么现在得到这个错误?非常感谢。

Absente.java:

package ro.radioamatori;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;


public class Absente extends Activity {

    private DbHelper_absente mHelpera;
    private SQLiteDatabase dataBase;

    private ArrayList<String> userId = new ArrayList<String>();
    private ArrayList<String> user_fName = new ArrayList<String>();
    private ArrayList<String> user_lName = new ArrayList<String>();
    private ArrayList<String> d = new ArrayList<String>();
    private ArrayList<String> stime = new ArrayList<String>();
    private ArrayList<String> etime = new ArrayList<String>();
    private ArrayList<String> freq = new ArrayList<String>();
    private ArrayList<String> mode = new ArrayList<String>();
    private ArrayList<String> station = new ArrayList<String>();
    private ArrayList<String> loc = new ArrayList<String>();
    private ArrayList<String> tqsl = new ArrayList<String>();
    private ArrayList<String> mqsl = new ArrayList<String>();
    private ArrayList<String> comm = new ArrayList<String>();

    private ListView userLista;
    private AlertDialog.Builder build;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.absente_listview);

        userLista = (ListView) findViewById(R.id.Lista);

        mHelpera = new DbHelper_absente(this);

        //add new record
        findViewById(R.id.btnAdda).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),
                        Adauga_absente.class);
                i.putExtra("update", false);
                startActivity(i);

            }
        });

        //click to update data
        userLista.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Intent i = new Intent(getApplicationContext(),
                        Adauga_absente.class);
                i.putExtra("Fname", user_fName.get(arg2));
                i.putExtra("Lname", user_lName.get(arg2));
                i.putExtra("ID", userId.get(arg2));
                i.putExtra("date", d.get(arg2));
                i.putExtra("date", stime.get(arg2));
                i.putExtra("date", etime.get(arg2));
                i.putExtra("date", freq.get(arg2));
                i.putExtra("date", mode.get(arg2));
                i.putExtra("date", station.get(arg2));
                i.putExtra("date", loc.get(arg2));
                i.putExtra("date", tqsl.get(arg2));
                i.putExtra("date", mqsl.get(arg2));
                i.putExtra("date", comm.get(arg2));
                i.putExtra("update", true);
                startActivity(i);

            }
        });

        //long click to delete data
        userLista.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    final int arg2, long arg3) {

                build = new AlertDialog.Builder(Absente.this);
                build.setTitle("Esti sigur ca vrei sa stergi? ");
                build.setMessage("Esti sigur ca vrei sa stergi aceasta materie si notele ?");
                build.setPositiveButton("Da",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                Toast.makeText(
                                        getApplicationContext(), " Materia " +
                                           user_fName.get(arg2) + " absenta din data " 
                                                 + user_lName.get(arg2)
                                                 + " au fost sterse din carnetul tau de elev.", 3000).show();

                                dataBase.delete(
                                        DbHelper.TABLE_NAME,
                                        DbHelper.KEY_ID + "="
                                                + userId.get(arg2), null);
                                displayData();
                                dialog.cancel();
                            }
                        });

                build.setNegativeButton("Nu",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = build.create();
                alert.show();

                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        displayData();
        super.onResume();
    }

    /**
     * displays data from SQLite
     */
    private void displayData() {
        dataBase = mHelpera.getWritableDatabase();
        Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
                + DbHelper.TABLE_NAME, null);

        userId.clear();
        user_fName.clear();
        user_lName.clear();
        d.clear();
        stime.clear();
        etime.clear();
        freq.clear();
        mode.clear();
        station.clear();
        loc.clear();
        tqsl.clear();
        mqsl.clear();
        comm.clear();
        if (mCursor.moveToFirst()) {
            do {
                userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
                user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
                user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
                d.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_D)));
                stime.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_STIME)));
                etime.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ETIME)));
                freq.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FREQ)));
                mode.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MODE)));
                station.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_STATION)));
                loc.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LOC)));
                tqsl.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_TQSL)));
                mqsl.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MQSL)));
                comm.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_COMM)));


            } while (mCursor.moveToNext());
        }
        NoteAdapter disadpt = new NoteAdapter(Absente.this,userId, user_fName, user_lName, d, stime, etime, freq, mode, station, loc, tqsl, mqsl, comm);
        userLista.setAdapter(disadpt);
        mCursor.close();
    }



}

Adauga_Absente.java:

package ro.radioamatori;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Adauga_absente extends Activity implements OnClickListener {
private Button btn_save;
private EditText edit_first,edit_last,d,st,et,f,m,s,l,tq,mq,c;
private DbHelper_absente mHelper;
private SQLiteDatabase dataBase;
private String id,fname,lname,date,stime,etime,freq,mode,station,loc,tqsl,mqsl,comm;
private boolean isUpdate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adauga_absente);

        btn_save=(Button)findViewById(R.id.save_btna);
        edit_first=(EditText)findViewById(R.id.frst_editTxta);
        edit_last=(EditText)findViewById(R.id.last_editTxta);
        d=(EditText)findViewById(R.id.d);
        st=(EditText)findViewById(R.id.st);
        et=(EditText)findViewById(R.id.et);
        f=(EditText)findViewById(R.id.f);
        m=(EditText)findViewById(R.id.m);
        s=(EditText)findViewById(R.id.s);
        l=(EditText)findViewById(R.id.l);
        tq=(EditText)findViewById(R.id.tq);
        mq=(EditText)findViewById(R.id.mq);
        c=(EditText)findViewById(R.id.c);

       isUpdate=getIntent().getExtras().getBoolean("update");
        if(isUpdate)
        {
            id=getIntent().getExtras().getString("ID");
            fname=getIntent().getExtras().getString("Fname");
            lname=getIntent().getExtras().getString("Lname");
            date=getIntent().getExtras().getString("Date");
            stime=getIntent().getExtras().getString("Stime");
            etime=getIntent().getExtras().getString("Etime");
            freq=getIntent().getExtras().getString("Freq");
            mode=getIntent().getExtras().getString("Mode");
            station=getIntent().getExtras().getString("Station");
            loc=getIntent().getExtras().getString("Loc");
            tqsl=getIntent().getExtras().getString("Tqsl");
            mqsl=getIntent().getExtras().getString("Mqsl");
            comm=getIntent().getExtras().getString("comm");
            edit_first.setText(fname);
            edit_last.setText(lname);
            d.setText(date);
            st.setText(stime);
            et.setText(etime);
            f.setText(freq);
            m.setText(mode);
            s.setText(station);
            l.setText(loc);
            tq.setText(tqsl);
            mq.setText(mqsl);
            c.setText(comm);


        }

         btn_save.setOnClickListener(this);

         mHelper=new DbHelper_absente(this);

    }

    // saveButton click event 
    public void onClick(View v) {
        fname=edit_first.getText().toString().trim();
        lname=edit_last.getText().toString().trim();
        date=d.getText().toString().trim();
        stime=st.getText().toString().trim();
        etime=et.getText().toString().trim();
        freq=f.getText().toString().trim();
        mode=m.getText().toString().trim();
        station=s.getText().toString().trim();
        loc=l.getText().toString().trim();
        tqsl=tq.getText().toString().trim();
        mqsl=mq.getText().toString().trim();
        comm=c.getText().toString().trim();
        if(fname.length()>0 && lname.length()>0 && date.length()>0 && stime.length()>0 && etime.length()>0 && freq.length()>0 && mode.length()>0 && station.length()>0 && loc.length()>0 && tqsl.length()>0 && mqsl.length()>0 && comm.length()>0)
        {
            saveData();
        }
        else
        {
            AlertDialog.Builder alertBuilder=new AlertDialog.Builder(Adauga_absente.this);
            alertBuilder.setTitle("Invalid Data");
            alertBuilder.setMessage("Please, Enter valid data");
            alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

                }
            });
            alertBuilder.create().show();
        }

    }

    /**
     * save data into SQLite
     */
    private void saveData(){
        dataBase=mHelper.getWritableDatabase();
        ContentValues values=new ContentValues();

        values.put(DbHelper.KEY_FNAME,fname);
        values.put(DbHelper.KEY_LNAME,lname );
        values.put(DbHelper.KEY_D,date );
        values.put(DbHelper.KEY_STIME,stime );
        values.put(DbHelper.KEY_ETIME,etime );
        values.put(DbHelper.KEY_FREQ,freq );
        values.put(DbHelper.KEY_MODE,mode );
        values.put(DbHelper.KEY_STATION,station );
        values.put(DbHelper.KEY_LOC,loc );
        values.put(DbHelper.KEY_TQSL,tqsl );
        values.put(DbHelper.KEY_MQSL,mqsl );
        values.put(DbHelper.KEY_COMM,comm );


        System.out.println("");
        if(isUpdate)
        {    
            //update database with new data 
            dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
        }
        else
        {
            //insert data into database
            dataBase.insert(DbHelper.TABLE_NAME, null, values);
        }
        //close database
        dataBase.close();
        finish();


    }

}

DBHelper.java:

package ro.radioamatori;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
    static String DATABASE_NAME="userdata";
    public static final String TABLE_NAME="user";
    public static final String KEY_FNAME="fname";
    public static final String KEY_ID="id";
    public static final String KEY_LNAME="lname";
    public static final String KEY_D="date";
    public static final String KEY_STIME="stime";
    public static final String KEY_ETIME="etime";
    public static final String KEY_FREQ="freq";
    public static final String KEY_MODE="mode";
    public static final String KEY_STATION="station";
    public static final String KEY_LOC="loc";
    public static final String KEY_TQSL="tqsl";
    public static final String KEY_MQSL="mqsl";
    public static final String KEY_COMM="comm";
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" TEXT, "+KEY_D+" TEXT, "+KEY_STIME+" TEXT, "+KEY_ETIME+" TEXT, "+KEY_FREQ+" TEXT, "+KEY_MODE+" TEXT, "+KEY_STATION+" TEXT, "+KEY_LOC+" TEXT, "+KEY_TQSL+" TEXT, "+KEY_MQSL+" TEXT, "+KEY_COMM+" TEXT)";
        db.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }

}

logcat的:

08-14 21:55:13.508: E/AndroidRuntime(1096): FATAL EXCEPTION: main
08-14 21:55:13.508: E/AndroidRuntime(1096): java.lang.RuntimeException: Unable to resume activity {ro.radioamatori/ro.radioamatori.Absente}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.os.Looper.loop(Looper.java:137)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at dalvik.system.NativeStart.main(Native Method)
08-14 21:55:13.508: E/AndroidRuntime(1096): 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.
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.database.CursorWindow.nativeGetString(Native Method)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.database.CursorWindow.getString(CursorWindow.java:434)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at ro.radioamatori.Absente.displayData(Absente.java:167)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at ro.radioamatori.Absente.onResume(Absente.java:137)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.Activity.performResume(Activity.java:5082)
08-14 21:55:13.508: E/AndroidRuntime(1096):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
08-14 21:55:13.508: E/AndroidRuntime(1096):     ... 12 more

1 个答案:

答案 0 :(得分:1)

这是你的错误。 super(context, DATABASE_NAME, null, 1);您始终将数据库版本设置为1!所以你永远不会更新数据库。改变为2。