使用4.4 Kitkat时,没有这样的列错误代码不起作用

时间:2016-11-29 19:54:49

标签: android sqlite android-4.4-kitkat

我在min android 5上创建了一个应用程序。但我想在4.4 android运行它。所以我将文件从第一个项目复制到第二个项目并更正了AndroidManifest等内容。当我正在编译第一个版本时,evrythink工作正常,但是对于应该使用android 4.4的版本我得到SQLite exeption"没有这样的列"。 代码:

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select, container, false);
        helper = new MobilnaPytajkaDatabase(getContext());
        db = helper.getWritableDatabase();
        cursor = db.query("QUIZ_TABLE", new String[]{"_id", "QUESTION"},
                "TYPE=0", null, null, null, null, null);
        lista = (ListView) view.findViewById(R.id.listview);
        if(cursor.moveToFirst()) {
            try {
                CursorAdapter ca = new SimpleCursorAdapter(getContext(),
                        R.layout.whitetextlayout1, cursor,
                        new String[]{"QUESTION"}, new int[]{R.id.text1},
                        0);
                lista.setAdapter(ca);
                lista.setOnItemClickListener(listener);
            } catch (SQLiteException e) {
                ;
            }
            return view;
        }
        else
            return inflater.inflate(R.layout.emptylist, container,false);
    }

SQLite open helper class:

package com.nieruchalski.mobilnapytajkaforkitkat;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MobilnaPytajkaDatabase extends SQLiteOpenHelper {
    public final static int DB_VERSION = 2;
    public final static String DB_NAME = "QUIZ_DATA";

    MobilnaPytajkaDatabase(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE QUIZ_TABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + "QUESTION TEXT"
        + "ANSWER TEXT"
        + "TYPE INTEGER);");
    }
}

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.nieruchalski.mobilnapytajkaforkitkat, PID: 2450
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nieruchalski.mobilnapytajkaforkitkat/com.nieruchalski.mobilnapytajkaforkitkat.selectQuiz}: android.database.sqlite.SQLiteException: no such column: TYPE (code 1): , while compiling: SELECT _id, QUESTION FROM QUIZ_TABLE WHERE TYPE=0
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
                      at android.app.ActivityThread.access$900(ActivityThread.java:150)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:168)
                      at android.app.ActivityThread.main(ActivityThread.java:5885)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                   Caused by: android.database.sqlite.SQLiteException: no such column: TYPE (code 1): , while compiling: SELECT _id, QUESTION FROM QUIZ_TABLE WHERE TYPE=0
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:921)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:532)
                      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:68)
                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1402)
                      at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1249)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1120)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1326)
                      at com.nieruchalski.mobilnapytajkaforkitkat.selectFragment.onCreateView(selectFragment.java:38)
                      at android.app.Fragment.performCreateView(Fragment.java:2238)
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1152)
                      at android.app.BackStackRecord.run(BackStackRecord.java:793)
                      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1539)
                      at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                      at android.app.Activity.performStart(Activity.java:6278)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2472)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:150) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:168) 
                      at android.app.ActivityThread.main(ActivityThread.java:5885) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

1 个答案:

答案 0 :(得分:2)

真的与Kitkat没什么关系。您只是在,中的列规范之间缺少逗号MobilnaPytajkaDatabase#onCreate()。在那里添加它们并卸载您的应用程序以删除旧数据库并创建一个新数据库。