Search code examples
androidandroid-studioandroid-activity

App crashes without any changes done


This post is related to Creating multiple Tables and inserting Data into them So I fixed the issue I had with only one table getting created. But now the app chrashes when I try to switch activities. My MainActivity is still working.

This is one of the classes/activities that crashes. The other one is similar, the only differences are the names of the buttons and 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();

}

-- and following the gradle file

// 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
}

--

and the crash info

    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

Thanks for the help in advance!


Solution

  • The cause of this exception is that you have something other then ?? INTEGER PRIMARY KEY AUTOINCREMENT for a column when creating the table.

    More specifically you have :-

    create table Lehrer_table(ID_LINTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
    

    This should likely be :-

    create table Lehrer_table(ID_L INTEGER PRIMARY KEY AUTOINCREMENT,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)
    

    i.e. a space has been added between ID_L and INTEGER so ID_LINTEGER becomes ID_L INTEGER.

    Saying that you using AUTOINCREMENT is very likely unnecessary and detrimental as it consumes more resources.

    All that AUTOINCREMENT does is ensure that the next id will be greater than the previous (it very likely will be anyway until you reached 9223372036854775807 rows). Should that number be reached with AUTOINCREMENT you would then not be able to insert rows as an insert would have an SQLITE_FULL exception. Whilst, there is a chance that without AUTOINCREMENT that a now unused id will be assigned.

    To quote the SQLIte documentation :-

    1. The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.

    2. In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.

    3. On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

    4. If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.

    As such I'd suggest using :-

    create table Lehrer_table(ID_L INTEGER PRIMARY KEY,LEHRERNAMETEXT,LEHRERKUERZELTEXT,LEHRERRAUMTEXT,LEHRERMAILTEXT)