应用程序崩溃而未做任何更改 [英] App crashes without any changes done

查看:126
本文介绍了应用程序崩溃而未做任何更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子与创建多个表并将数据插入其中 因此,我解决了仅创建一个表时遇到的问题.但是现在,当我尝试切换活动时,应用程序崩溃了.我的MainActivity仍在工作.

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();

}

- 并遵循gradle文件

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

-

和崩溃信息

    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

感谢您的提前帮助!

推荐答案

此异常的原因是,在创建表时,列中除了?? INTEGER PRIMARY KEY AUTOINCREMENT之外还有其他内容.

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

更具体地说,您有:-

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 之间添加了一个空格,因此 ID_LINTEGER 变为 ID_L INTEGER .

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

说您使用AUTOINCREMENT很可能是不必要和有害的,因为它消耗了更多资源.

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

AUTOINCREMENT所做的全部工作是确保下一个 id 大于上一个(很可能会一直到您到达9223372036854775807行).如果使用AUTOINCREMENT达到该数字,则您将无法插入行,因为插入会出现 SQLITE_FULL 异常.同时,如果没有AUTOINCREMENT,有可能分配一个现在未使用的ID.

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.

引用SQLIte文档:-

To quote the SQLIte documentation :-

  1. AUTOINCREMENT关键字强加了额外的CPU,内存,磁盘空间和 磁盘I/O开销,如果没有严格要求,则应避免.它是 通常不需要.

  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.

在SQLite中,类型为INTEGER PRIMARY KEY的列是该对象的别名. ROWID(在WITHOUT ROWID表中除外)始终为64位带符号 整数.

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.

在INSERT上,如果ROWID或INTEGER PRIMARY KEY列不是 明确地给定一个值,那么它将自动用 未使用的整数,通常比当前最大的ROWID多一 使用.不论AUTOINCREMENT是否为真 使用了关键字.

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.

如果在INTEGER PRIMARY KEY之后出现AUTOINCREMENT关键字,则表明 更改自动ROWID分配算法以防止重用 数据库生命周期内的ROWID数量.换句话说, AUTOINCREMENT的目的是防止ROWID的重用 先前删除的行.

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.

因此,我建议使用:-

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

这篇关于应用程序崩溃而未做任何更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆