这篇文章与Creating multiple Tables and inserting Data into them有关 所以我修复了我只有一个表被创建的问题。但是现在当我尝试切换活动时应用程序会崩溃。我的MainActivity仍然有效。
这是崩溃的类/活动之一。另一个是相似的,唯一的区别是按钮和textedits的名称。
public class FachErstellen extends AppCompatActivity {
DatabaseHelper myDb;
EditText editTextFachName;
EditText editTextFachKuerzel;
EditText editTextFachRaum;
EditText editTextFachLehrer;
Button buttonFachSpeichern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fach_erstellen_activity);
myDb = new DatabaseHelper(this);
myDb.fuegeNeueTabellenHinzu();
editTextFachName = (EditText) findViewById(R.id.editTextFachName);
editTextFachKuerzel = (EditText) findViewById(R.id.editTextFachKuerzel);
editTextFachRaum = (EditText) findViewById(R.id.editTextFachRaum);
editTextFachLehrer = (EditText) findViewById(R.id.editTextFachLehrer);
buttonFachSpeichern = (Button) findViewById(R.id.buttonFachSpeichern);
addFach();
zeigeFaecher();
}
public void addFach(){
buttonFachSpeichern.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean istGespeichert = myDb.speichereFach(editTextFachName.getText().toString(),
editTextFachKuerzel.getText().toString(),
editTextFachRaum.getText().toString(),
editTextFachLehrer.getText().toString());
if (istGespeichert==true){
Toast.makeText(FachErstellen.this, "Fach wurde gespeichert.", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(FachErstellen.this, "Fach konnte nicht gespeichert werden.", Toast.LENGTH_LONG).show();
}
}
});
}
public void zeigeFaecher(){
buttonFaecherAnzeigen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = myDb.zeigeFaecher();
if (res.getCount() == 0) {
zeigeNachricht("Fehler", "Keine Fächer gefunden");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()){
buffer.append("ID:" + res.getString(0)+"\n");
buffer.append("Fach: " + res.getString(1)+"\n");
buffer.append("Kürzel: " + res.getString(2)+"\n");
buffer.append("Raum: " + res.getString(3)+"\n");
buffer.append("Lehrer: " + res.getString(4)+"\n\n");
}
zeigeNachricht("Fächer", buffer.toString());
}
});
}
public void zeigeNachricht(String title, String Nachricht){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Nachricht);
builder.show();
}
- 并遵循gradle文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
-
和崩溃信息
Increasing code cache capacity to 128KB
05-29 15:15:28.150 15952-15952/jannikokan.de.stundenplan D/MeineAPP: DB angelegt
05-29 15:15:28.154 15952-15952/jannikokan.de.stundenplan E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
05-29 15:15:28.154 15952-15952/jannikokan.de.stundenplan D/AndroidRuntime: Shutting down VM
05-29 15:15:28.155 15952-15952/jannikokan.de.stundenplan E/AndroidRuntime: FATAL EXCEPTION: main
Process: jannikokan.de.stundenplan, PID: 15952
java.lang.RuntimeException: Unable to start activity ComponentInfo{jannikokan.de.stundenplan/jannikokan.de.stundenplan.LehrerErstellen}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: create table Lehrer_table(ID_LINTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: create table Lehrer_table(ID_LINTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at jannikokan.de.stundenplan.DatabaseHelper.CheckeUndErstelleTabelle(DatabaseHelper.java:114)
at jannikokan.de.stundenplan.DatabaseHelper.erstelleTabellenDieNichtExistieren(DatabaseHelper.java:98)
at jannikokan.de.stundenplan.DatabaseHelper.fuegeNeueTabellenHinzu(DatabaseHelper.java:80)
at jannikokan.de.stundenplan.LehrerErstellen.onCreate(LehrerErstellen.java:36)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
05-29 15:15:28.155 1595-1606/? W/ActivityManager: Force finishing activity jannikokan.de.stundenplan/.LehrerErstellen
05-29 15:15:28.158 1595-1606/? W/ActivityManager: Force finishing activity jannikokan.de.stundenplan/.SliderActivityActivity
05-29 15:15:28.196 1595-1636/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-29 15:15:28.196 1595-1636/? D/OpenGLRenderer: Swap behavior 1
05-29 15:15:28.196 1595-1636/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-29 15:15:28.196 1595-1636/? D/OpenGLRenderer: Swap behavior 0
05-29 15:15:28.200 1595-1636/? D/EGL_emulation: eglCreateContext: 0xa350f6c0: maj 2 min 0 rcv 2
05-29 15:15:28.211 1595-1636/? D/EGL_emulation: eglMakeCurrent: 0xa350f6c0: ver 2 0 (tinfo 0x974e53b0)
05-29 15:15:28.218 1595-1636/? D/EGL_emulation: eglMakeCurrent: 0xa350f6c0: ver 2 0 (tinfo 0x974e53b0)
05-29 15:15:28.660 1595-1608/? W/ActivityManager: Activity pause timeout for ActivityRecord{17375b u0 jannikokan.de.stundenplan/.LehrerErstellen t103 f}
05-29 15:15:28.779 1996-2119/? D/EGL_emulation: eglMakeCurrent: 0xa5e052a0: ver 2 0 (tinfo 0xa5e03630)
05-29 15:15:29.294 1996-2119/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
05-29 15:15:31.327 1353-1378/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 7930259 , only wrote 7777440
提前感谢您的帮助!
答案 0 :(得分:0)
此异常的原因是您在创建表时为列添加了1.5
以外的其他内容。
更具体地说,你有: -
?? INTEGER PRIMARY KEY AUTOINCREMENT
这应该是: -
create table Lehrer_table(ID_LINTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
即。在 create table Lehrer_table(ID_L INTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
和 ID_L
之间添加了一个空格,因此 INTEGER
变为 {{ 1}} 强>
说你使用ID_LINTEGER
很可能是不必要的,因为它消耗了更多的资源而有害。
ID_L INTEGER
所做的只是确保下一个 ID 将大于之前的版本(在您达到9223372036854775807行之前,它很可能会一直存在)。如果使用AUTOINCREMENT
达到该数字,则您将无法插入行,因为插入将具有 SQLITE_FULL 异常。同时,如果没有AUTOINCREMENT
,可能会分配一个现在未使用的ID。
引用SQLIte文档: -
AUTOINCREMENT关键字会占用额外的CPU,内存,磁盘空间和 磁盘I / O开销,如果不是严格需要,应该避免。它是 通常不需要。
在SQLite中,类型为INTEGER PRIMARY KEY的列是其中的别名 ROWID(除非在WITHOUT ROWID表中),它总是64位签名 整数。
在INSERT上,如果不是ROWID或INTEGER PRIMARY KEY列 明确给出一个值,然后它会自动填充一个 未使用的整数,通常比当前最大的ROWID多一个 使用。无论AUTOINCREMENT是否正确,都是如此 使用关键字。
- 醇>
如果在INTEGER PRIMARY KEY之后出现AUTOINCREMENT关键字,那么 更改自动ROWID分配算法以防止重用 在数据库的生命周期中的ROWID。换句话说, AUTOINCREMENT的目的是防止重用ROWID 以前删除的行。
因此我建议使用: -
AUTOINCREMENT