java.lang.RuntimeException:无法启动活动ComponentInfo,编译时语法错误在“,”附近

时间:2018-12-15 23:32:21

标签: android-studio

使用公共列表getArzt(),我尝试从我的SQ Lite数据库中获取每个“ Arzt”。如您所见,“ Arzt”中的每个对象都有一个名称arzttyp,straße+ hsnr。等等... 当我尝试运行我的项目时,应用程序崩溃并出现标题错误。

为清楚起见:我打算在这个项目中创建一个应用程序,在其中可以通过在搜索栏中键入“ Arzttyp”来搜索“ Arzt”类型的不同对象。

如果有人能够帮助我解决这个问题,那就太好了!

这是发生错误的类:

public List<Arzt> getArzt()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"Name", "Straße+Hsnr.", "PLZ", "Ort", "Telefon", "Arzttyp"};
    String tableName = "Aerzte";

    qb.setTables(tableName);
    Cursor cursor = qb.query(db, sqlSelect, null, null, null, null, null);
    List<Arzt> result = new ArrayList<>();
    if (cursor.moveToFirst()) {
        do {
            Arzt arzt = new Arzt();
            arzt.setAtyp(cursor.getString(cursor.getColumnIndex( "Arzttyp")));
            arzt.setName(cursor.getString(cursor.getColumnIndex(  "Name")));
            arzt.setStraße(cursor.getString(cursor.getColumnIndex( "Straße+Hsnr.")));
            arzt.setOrt(cursor.getString(cursor.getColumnIndex( "Ort")));
            arzt.setPlz(cursor.getInt(cursor.getColumnIndex( "PLZ")));
            arzt.setTel(cursor.getString(cursor.getColumnIndex( "Telefon")));

            result.add(arzt);
        } while (cursor.moveToNext());
    }
    return result;

这是Logcat输出:

I/SQLiteAssetHelper: successfully opened database aerzte.db
E/SQLiteLog: (1) near ",": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.christopher.rztesuche, PID: 6667
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.christopher.rztesuche/com.example.christopher.rztesuche.Main2}: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: SELECT Name, Straße+Hsnr., PLZ, Ort, Telefon, Arzttyp FROM Aerzte
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: SELECT Name, Straße+Hsnr., PLZ, Ort, Telefon, Arzttyp FROM Aerzte
        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.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
        at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:399)
        at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:294)
        at com.example.christopher.rztesuche.Database.Database.getArzt(Database.java:37)
        at com.example.christopher.rztesuche.Main2.onCreate(Main2.java:94)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Application terminated.

1 个答案:

答案 0 :(得分:0)

您的问题是该列:Straße+Hsnr.
其名称包含非法字符:+.
您可以像这样引用它:[Straße+Hsnr.],以便它不会引发错误。
因此更改为:

String[] sqlSelect = {"Name", "[Straße+Hsnr.]", "PLZ", "Ort", "Telefon", "Arzttyp"};

这:

arzt.setStraße(cursor.getString(cursor.getColumnIndex( "[Straße+Hsnr.]")));

修改
更改为此,看看它是否正常工作:

    Arzt arzt = new Arzt();
    arzt.setAtyp(cursor.getString(5));
    arzt.setName(cursor.getString(0));
    arzt.setStraße(cursor.getString(1));
    arzt.setOrt(cursor.getString(3));
    arzt.setPlz(cursor.getInt(2));
    arzt.setTel(cursor.getString(4));