私はandroid-projectにsqliteデータベースを持っています。昨日まではすべてがうまく行きましたが、今、私は次のエラーを取得する:次のクラスでgetReadableDatabase
を呼び出すときにgetReadableDatabaseでのIllegalStateException
FATAL EXCEPTION: main Process: com.jurtz.android.ichhabnochnie, PID: 7720
java.lang.RuntimeException: Unable to resume activity {com.jurtz.android.ichhabnochnie/com.jurtz.android.ichhabnochnie.customEntryActivity}: 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.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.jurtz.android.ichhabnochnie.database.databaseManager.onUpgrade(databaseManager.java:69)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.jurtz.android.ichhabnochnie.customEntryActivity.onResume(customEntryActivity.java:106)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6327)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
このエラーが発生します。
public class customEntryActivity extends AppCompatActivity {
TextView lblCustomEntryInfo;
Button cmdEnterCustomEntry;
Button cmdCancelCustomEntry;
EditText txtCustomEntry;
RelativeLayout customEntryActivityLayout;
Activity activity;
private SQLiteDatabase db;
private databaseManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_entry);
dbManager = new databaseManager(this);
activity = this;
customEntryActivityLayout = (RelativeLayout)findViewById(R.id.customEntryActivityLayout);
customEntryActivityLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
});
lblCustomEntryInfo = (TextView)findViewById(R.id.lblCustomEntryInfo);
lblCustomEntryInfo.setText("Ich hab noch nie...");
cmdCancelCustomEntry = (Button)findViewById(R.id.cmdCustomEntryCancel);
cmdCancelCustomEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.close();
finish();
}
});
cmdEnterCustomEntry = (Button)findViewById(R.id.cmdCustomEntryEnter);
cmdEnterCustomEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (txtCustomEntry.getText().toString().length() > 0) {
Date today = new Date();
String text = txtCustomEntry.getText().toString().replace("'","\'");
Message msg = new Message(txtCustomEntry.getText().toString(), today, "CUSTOM");
String sql = MessageHelper.getInputCommand(msg, databaseManager.getTableName());
try {
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "Eintrag hinzugefügt", Toast.LENGTH_SHORT).show();
} catch (SQLException sqlEx) {
Toast.makeText(getApplicationContext(), "Fehler beim Eintrag in die Datenbank", Toast.LENGTH_SHORT).show();
}
txtCustomEntry.setText("");
} else {
Toast.makeText(getApplicationContext(),"Keine Eingabe vorhanden",Toast.LENGTH_SHORT).show();
}
}
});
txtCustomEntry = (EditText)findViewById(R.id.txtCustomEntry);
}
// DB-Methoden
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onResume() {
super.onResume();
db = dbManager.getReadableDatabase();
// Toast.makeText(getApplicationContext(),"DB geöffnet",Toast.LENGTH_LONG).show();
}
}
私は何人かの人々が同様の問題があったが、ことを見ました
public class databaseManager extends SQLiteOpenHelper {
private static final String dbName = "db_IchHabNochNie";
// version 9: Update 12.7.
private static final int dbVersion = 11;
private static final String tableName = "message";
private static final String createTable = "CREATE TABLE "+tableName+"(" +
"text VARCHAR(100) PRIMARY KEY, " +
"author VARCHAR(6), " +
"date_added VARCHAR(10)" +
");";
private static final String dropTable = "DROP TABLE IF EXISTS "+tableName+";";
public static final String SELECT_USER_MESSAGES = "SELECT text FROM "+tableName+" WHERE author='CUSTOM';";
public static final String SELECT_SYSTEM_MESSAGES = "SELECT text FROM "+tableName+" WHERE author='SYSTEM';";
public static final String SELECT_ALL_MESSAGES = "SELECT text FROM "+tableName+";";
public databaseManager(Context context) {
super(context,dbName,null,dbVersion);
}
public databaseManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTable);
HashSet<Message> messages = MessageHelper.getMessages();
for(Message msg : messages) {
db.execSQL(MessageHelper.getInputCommand(msg,tableName));
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Backup and Re-Insert CUSTOMs
ArrayList<Message> customs = new ArrayList<>();
boolean customsExist = false;
Cursor c = db.rawQuery(SELECT_USER_MESSAGES, null);
if(c.getCount() > 0) {
customsExist = true;
String text;
String author;
String date;
if (c.moveToFirst()) {
while (c.isAfterLast() == false) {
text = c.getString(c.getColumnIndex("text"));
date = c.getString(c.getColumnIndex("date_added"));
author = c.getString(c.getColumnIndex("author"));
Message msg = new Message(text, date, author);
customs.add(msg);
c.moveToNext();
}
}
}
db.execSQL(dropTable);
db.execSQL(createTable);
if(customsExist) {
// Einträge wieder einfügen
for(int i = 0; i<customs.size(); i++) {
String sql = MessageHelper.getInputCommand(customs.get(i),tableName);
try {
db.execSQL(sql);
} catch (SQLException sqlEx) {
// To be implemented
}
}
}
HashSet<Message> messages = MessageHelper.getMessages();
for(Message msg : messages) {
db.execSQL(MessageHelper.getInputCommand(msg,tableName));
}
}
public static String getTableName() {
return tableName;
}
0:私は現在...
のDatabaseManagerをエラーから来ているし、それを引き起こしているものを任意のアイデアを持っていません
}
ありがとうございます!
スタックトレース全体を送信してください。 – CommonsWare
なぜ "db = dbManager.getReadableDatabase();"が必要ですか? inside onResume()? – user3765370
@ user3765370これを追加して、接続が利用可能であることを確認し、onClickメソッドで使用するための 'db'を取得します。 – Marcel