我正在创建一个使用SQLiteDatabase来存储项目的应用程序ListView
。但是,ListView
从数据库中填充数据时出现错误,应用崩溃。
这是我的数据库-
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class NotesDatabase extends SQLiteOpenHelper {
private static final String NOTE_NAME = "name";
private static final String NOTE_ID = "id";
private static final String NOTE_SUBJECT = "subject";
private static final String DATABASE_NAME = "notesDatabase";
private static final String TABLE_NOTES = "notes";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + "("
+ NOTE_ID + " INTEGER PRIMARY KEY," + NOTE_NAME + " TEXT,"
+ NOTE_SUBJECT + " TEXT" + ")";
public NotesDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_NOTES_TABLE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXSISTS topics");
onCreate(db);
}
public void addNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues notes = new ContentValues();
notes.put(NOTE_NAME, note.getNote());
notes.put(NOTE_SUBJECT, note.getSubject());
db.insert(TABLE_NOTES, null, notes);
db.close();
}
public void removeNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NOTES, NOTE_NAME + " = ?",
new String[] {note.getNote()});
db.close();
}
public ArrayList<Note> getNotes(){
ArrayList<Note> notes = new ArrayList<Note>();
String selectQuery = "SELECT * FROM " + TABLE_NOTES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery(selectQuery, null);
if(cur.moveToFirst()) {
do {
Note note = new Note();
note.setId(Integer.parseInt(cur.getString(0)));
note.setNote(cur.getString(cur.getColumnIndex(NOTE_NAME)));
note.setSubject(cur.getString(cur.getColumnIndex(NOTE_SUBJECT)));
notes.add(note);
} while(cur.moveToNext());
}
db.close();
return notes;
}
public void updateNote(String old_name, String new_name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues topic = new ContentValues();
topic.put(NOTE_NAME, new_name);
db.update(TABLE_NOTES, topic, NOTE_NAME + " = ?",
new String[] {old_name});
db.close();
}
}
Note.java-
public class Note {
String _Note;
int _id;
String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Note() {
}
public Note(int id, String Note, String Subject) {
this._id = id;
this._Note = Note;
this.subject = Subject;
}
public Note(String Note, String Subject) {
this._Note = Note;
this.subject = Subject;
}
public Note(String Note) {
this._Note = Note;
}
public String getNote() {
return _Note;
}
public void setNote(String _Note) {
this._Note = _Note;
}
public int getId() {
return _id;
}
public void setId(int _id) {
this._id = _id;
}
}
NotesDatabase.java-
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Notes_page extends Activity {
ArrayList<Note> notes = new ArrayList<Note>();
NotesDatabase ndb = new NotesDatabase(this);
private NoteAdapter adapter;
private ListView notes_list;
private TextView subject_display;
String subject;
int requestCode = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_page);
notes_list = (ListView) findViewById(R.id.notes);
ndb.getWritableDatabase();
notes = ndb.getNotes();
adapter = new NoteAdapter(this, notes);
subject = getIntent().getStringExtra("SUBJECT_NAME");
notes_list.setAdapter(adapter);
subject_display = (TextView) findViewById(R.id.subject_name);
subject_display.setText(subject);
}
public void onClickAddNewNote(View v) {
startActivityForResult(new Intent("com.Swap.Add_New_Note"), requestCode);
}
public void onActivityResult(int request_Code, int result_Code, Intent i) {
if(request_Code == requestCode) {
if(result_Code == RESULT_OK) {
Note mNote = new Note(i.getStringExtra("NOTE"), getIntent().getStringExtra("SUBJECT_NAME"));
ndb.getWritableDatabase();
ndb.addNote(mNote);
Log.d("AddNote", "Note Succesfully Added to Database");
adapter.add(mNote);
adapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), mNote.getNote(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_notes_page, menu);
return true;
}
}
这是LogCat错误日志-
04-13 22:17:10.766: E/linker(25692): load_library(linker.cpp:759): library "libmaliinstr.so" not found
04-13 22:17:14.816: E/CursorWindow(25692): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 2 columns.
04-13 22:17:14.869: E/AndroidRuntime(25692): FATAL EXCEPTION: main
04-13 22:17:14.869: E/AndroidRuntime(25692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Swap.RR/com.Swap.RR.Notes_page}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread.access$600(ActivityThread.java:174)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.os.Handler.dispatchMessage(Handler.java:107)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.os.Looper.loop(Looper.java:194)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread.main(ActivityThread.java:5409)
04-13 22:17:14.869: E/AndroidRuntime(25692): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 22:17:14.869: E/AndroidRuntime(25692): at java.lang.reflect.Method.invoke(Method.java:525)
04-13 22:17:14.869: E/AndroidRuntime(25692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 22:17:14.869: E/AndroidRuntime(25692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
04-13 22:17:14.869: E/AndroidRuntime(25692): at dalvik.system.NativeStart.main(Native Method)
04-13 22:17:14.869: E/AndroidRuntime(25692): 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.
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.database.CursorWindow.nativeGetString(Native Method)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.database.CursorWindow.getString(CursorWindow.java:434)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
04-13 22:17:14.869: E/AndroidRuntime(25692): at com.Swap.RR.NotesDatabase.getNotes(NotesDatabase.java:78)
04-13 22:17:14.869: E/AndroidRuntime(25692): at com.Swap.RR.Notes_page.onCreate(Notes_page.java:41)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.Activity.performCreate(Activity.java:5122)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
04-13 22:17:14.869: E/AndroidRuntime(25692): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
04-13 22:17:14.869: E/AndroidRuntime(25692): ... 11 more
请帮助我找出我的代码出了什么问题。提前致谢!!
您的主题行具有误导性。您的堆栈跟踪中的异常说
Couldn't read row 0, col -1 from CursorWindow
这实际上意味着getColumnIndex()
无法在中找到指定的列,Cursor
并且-1作为列索引返回。
您的选择SELECT *
包含表中的所有列。
您CREATE TABLE
似乎包含了您要求getColumnIndex()
in中的索引的所有列getNotes()
。
您可能已经添加了一个列,但是测试设备仍然具有不带该列的数据库文件。只需卸载您的应用程序或清除其数据即可删除旧的数据库文件,并onCreate()
在下次运行时重新创建数据库。
问题内容: 使用数据库时出现问题。当我运行SQLView.java时,出现一个致命异常: 希望有人可以帮助我。我将发布所有与数据库相关的代码。当您需要更多信息时,请告诉我,不要犹豫,索取更多信息! 我认为我的专栏有问题,无法找出原因。 另外,我已经阅读了该网站上的所有相关问题,但是我找不到应该在何处声明我的列以及要添加/更改的内容。有人知道怎么做吗?:) 首先:我的数据库类: SQLite类,用于
我正在开发一个应用程序,下载一些文件,并将其文本保存在文件内容字段中的数据库中。文件大小可以从一些KB到10 MB不等。该应用程序在保存时适用于所有尺寸。在长文件内容记录上使用select语句时出现问题。它给 java.lang.非法状态异常:无法从CursorWindow读取行0, col0 获取这样的行时。字段内容大小是否有限制?如果是这样,那么为什么它让我们在检索时保存并给出错误呢?这是我截
我尝试使用上下文菜单从listview中删除数据,得到了java.lang.IllegalStateException:Could not read row 0,col 2 from CursorWindow。在从游标访问数据之前,请确保游标已正确初始化。 第一行错误AssignmentRecord.SetAssid(Cursor.GetString(2)); 第二个错误是List records
我想分享 /Internal存储/aaa/bbb.dat文件到另一个应用程序通过FileProvider。但它出错了。 我已经将路径放入了provider\u path。xml并将提供程序放入清单文件中。 我希望gmail或midrop/shareme能够打开和bbb。dat文件附呈。但它出错了 显示 提供者路径。xml 我是根据这些帖子编写代码的 Android:自定义外部存储文件夹上的文件提供
我已经创建了一个非常简单的数据库android应用程序。它接受输入并显示结果。添加按钮用于添加输入,删除按钮用于删除存储在SQLite数据库中的输入。我在Android SQLite中的光标指向正确,但仍然面临错误:由:java.lang.IllegalStateException引起:无法从CursorWindow读取第0,col-1行。在从中访问数据之前,请确保光标已正确初始化。以下是游标部分