gpt4 book ai didi

java - 无法从 CursorWindow 错误中读取第 0 行、第 -1 列

转载 作者:行者123 更新时间:2023-12-01 12:38:50 26 4
gpt4 key购买 nike

我以前使用过 SQLDB,但只保存了 3 项。现在我想添加更多内容,但出现此错误,但无法解决。我不明白我的光标在哪里变坏并且不再工作了。谁能解释一下为什么我现在会收到此错误?非常感谢。

缺席.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

最佳答案

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

关于java - 无法从 CursorWindow 错误中读取第 0 行、第 -1 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com