为什么我的Android应用程序强制关闭?

时间:2013-01-25 15:23:34

标签: java android eclipse sqlite

我有一个使用SQLite数据库的android应用程序。但当它到达select语句时,它会强制关闭。我有一个用于执行此操作的类文件:

package com.thesis.menubook;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class DBConnect {

        int id = 0;
        public static final String KEY_ROWID = "_id";
        public static final String KEY_IP = "saved_ip_address";
        private static final String TAG = "DBConnect";

        private static final String DATABASE_NAME = "MenuBook";
        private static final String DATABASE_TABLE_1 = "ipaddress";
       //private static final String DATABASE_TABLE_2 = "menudb";
       //private static final String DATABASE_TABLE_3 = "recipelist";
        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE_TABLE_1 =
            "CREATE TABLE ipaddress (_id integer primary key autoincrement, " +
                                 "saved_ip_address text not null " +
                                 "); ";
        private static final String DATABASE_CREATE_TABLE_2 =
            "CREATE TABLE menudb (menu_ID varchar primary key not null, " +
                                 "menu_name longtext, " +
                                 "menu_price double null default, " +
                                 "menu_description longtext, " +
                                 "menu_category text, " +
                                 "menu_status text " +
                                 "); ";
        private static final String DATABASE_CREATE_TABLE_3 =
            "CREATE TABLE recipelist (recipe_ID integer primary key not null autoincrement, " +
                                 "menu_ID varchar null default, " +
                                 "stock_ID varchar null default, " +
                                 "recipe_quantity double null defualt " +
                                 ");" ;

        private final Context context;

        private static DatabaseHelper DBHelper;
        private static SQLiteDatabase db;

        public DBConnect(Context ctx)
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }

        public static class DatabaseHelper extends SQLiteOpenHelper
        {
            DatabaseHelper(Context context)
            {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db)
            {
                db.execSQL(DATABASE_CREATE_TABLE_1);
                db.execSQL(DATABASE_CREATE_TABLE_2);
                db.execSQL(DATABASE_CREATE_TABLE_3);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion,
                                  int newVersion)
            {
                Log.w(TAG, "Upgrading database from version " + oldVersion
                      + " to "
                      + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS ipaddress");
                db.execSQL("DROP TABLE IF EXISTS menudb");
                db.execSQL("DROP TABLE IF EXISTS recipelist");
                onCreate(db);
            }

        }

        //---opens the database---
        public DBConnect open() throws SQLException
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }

        //---closes the database---
        public void close()
        {
            DBHelper.close();
        }

        //---insert a title into the database---
        public long insertIPAddress(String ipaddress)
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_IP, ipaddress);
            return db.insert(DATABASE_TABLE_1, null, initialValues);
        }

        public String getIP()
        {
            String retrievedIP;

            String query = "SELECT saved_ip_address FROM ipaddress WHERE _id = (SELECT MAX(_id) FROM ipaddress)";
            Cursor cursor = db.rawQuery(query, null);
            retrievedIP= cursor.getString(cursor.getColumnIndex("saved_ip_address"));
            cursor.close();
            return retrievedIP;
        }

        public List<ColumnValue[]> select(final String query)
        {
            List<ColumnValue[]> result = null;
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst())
            {
                result = new ArrayList<ColumnValue[]>();

                do
                {
                    int columns = cursor.getColumnCount();
                    ColumnValue[] cvarray = new ColumnValue[columns];

                    for (int i=0; i<columns; i++)
                    {
                        String key = cursor.getColumnName(i);
                        String value = cursor.getString(i);
                        ColumnValue cv = new ColumnValue(key, value);
                        cvarray[i] = cv;
                    }

                    result.add(cvarray);
                }
                while (cursor.moveToNext());
            }

            cursor.close();

            return result;
        }

        public class ColumnValue
        {
            public String column, value;

            public ColumnValue(String c, String v)
            {
                column = c; value = v;
            }
        }


}

现在,当我查看LogCat时,我注意到“红色”错误,我真的不知道颜色的含义,从应用程序开始使用onCreate中的数据库开始。

我在sql上看不到任何错误。这是因为Eclipse,我的系统,还是我的create语句确实存在错误?指出来!

这是我的LogCat,错误的开始以粗体显示。

谢谢

01-25 22:58:30.245: DEBUG/AndroidRuntime(521): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-25 22:58:30.248: DEBUG/AndroidRuntime(521): CheckJNI is ON
01-25 22:58:34.103: DEBUG/AndroidRuntime(521): Calling main entry com.android.commands.pm.Pm
01-25 23:03:39.518: INFO/ActivityManager(83): Starting: Intent { cmp=com.thesis.menubook/.ChooseTable } from pid 541
01-25 23:03:40.161: WARN/ActivityManager(83): Activity pause timeout for ActivityRecord{4078a9e0 com.thesis.menubook/.IPAddress}

01-25 23:03:40.769:INFO / SqliteDatabaseCpp(541):sqlite返回:错误代码= 1,msg =接近“autoincrement”:语法错误,db = / data / data / com.thesis .menubook /数据库/ MenuBook

01-25 23:03:40.849: WARN/System.err(541): android.database.sqlite.SQLiteException: near "autoincrement": syntax error
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:89)
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
01-25 23:03:40.939: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
01-25 23:03:40.939: WARN/System.err(541):     at com.thesis.menubook.DBConnect$DatabaseHelper.onCreate(DBConnect.java:68)
01-25 23:03:40.958: WARN/System.err(541):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-25 23:03:40.958: WARN/System.err(541):     at com.thesis.menubook.DBConnect.open(DBConnect.java:89)
01-25 23:03:40.958: WARN/System.err(541):     at com.thesis.menubook.IPAddress$InsertIPAddress$1.run(IPAddress.java:92)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:03:40.978: WARN/System.err(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:03:41.046: WARN/System.err(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:03:41.049: WARN/System.err(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:03:41.068: WARN/System.err(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:03:41.078: WARN/System.err(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:03:41.124: WARN/System.err(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:03:49.440: INFO/ActivityManager(83): Displayed com.thesis.menubook/.ChooseTable: +9s277ms
01-25 23:03:49.678: WARN/ActivityManager(83): Launch timeout has expired, giving up wake lock!
01-25 23:03:50.244: WARN/ActivityManager(83): Activity idle timeout for ActivityRecord{408897c0 com.thesis.menubook/.ChooseTable}
01-25 23:03:51.832: WARN/InputConnectionWrapper.ICC(136): Timed out waiting on IInputContextCallback
01-25 23:03:53.015: INFO/InputConnectionWrapper.ICC(136): Got out-of-sequence callback 189 (expected 190) in setCursorCapsMode, ignoring.
01-25 23:04:47.188: INFO/SqliteDatabaseCpp(541): sqlite returned: error code = 1, msg = near "autoincrement": syntax error, db=/data/data/com.thesis.menubook/databases/MenuBook
01-25 23:04:47.258: WARN/System.err(541): android.database.sqlite.SQLiteException: near "autoincrement": syntax error
01-25 23:04:47.327: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
01-25 23:04:47.329: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:89)
01-25 23:04:47.359: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
01-25 23:04:47.359: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
01-25 23:04:47.389: WARN/System.err(541):     at com.thesis.menubook.DBConnect$DatabaseHelper.onCreate(DBConnect.java:68)
01-25 23:04:47.398: WARN/System.err(541):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-25 23:04:47.398: WARN/System.err(541):     at com.thesis.menubook.DBConnect.open(DBConnect.java:89)
01-25 23:04:47.448: WARN/System.err(541):     at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:102)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:04:47.458: WARN/System.err(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:04:47.471: WARN/System.err(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:04:47.471: WARN/System.err(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:04:47.499: WARN/System.err(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:04:47.499: WARN/System.err(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:04:47.499: WARN/System.err(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:04:47.751: DEBUG/AndroidRuntime(541): Shutting down VM
01-25 23:04:47.751: WARN/dalvikvm(541): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-25 23:04:47.838: ERROR/AndroidRuntime(541): FATAL EXCEPTION: main
01-25 23:04:47.838: ERROR/AndroidRuntime(541): java.lang.IllegalArgumentException: Host name may not be null
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.HttpHost.<init>(HttpHost.java:83)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.thesis.menubook.JSONParser.makeHttpRequest(JSONParser.java:62)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:119)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:04:47.950: WARN/ActivityManager(83):   Force finishing activity com.thesis.menubook/.ChooseTable
01-25 23:04:48.575: WARN/ActivityManager(83): Activity pause timeout for ActivityRecord{408897c0 com.thesis.menubook/.ChooseTable}
01-25 23:04:52.609: INFO/Process(541): Sending signal. PID: 541 SIG: 9
01-25 23:04:53INFO.259: /ActivityManager(83): Process com.thesis.menubook (pid 541) has died.
01-25 23:04:53.279: ERROR/InputDispatcher(83): channel '4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
01-25 23:04:53.279: ERROR/InputDispatcher(83): channel '4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Channel is unrecoverably broken and will be disposed!
01-25 23:04:53.420: INFO/WindowManager(83): WIN DEATH: Window{4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:04:54.193: INFO/WindowManager(83): WIN DEATH: Window{408347c8 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:04:54.819: INFO/WindowManager(83): WIN DEATH: Window{407c8fa8 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:04:55.620: INFO/WindowManager(83): WINDOW DIED Window{4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:04:55.690: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel.  fd=187, events=0x8
01-25 23:04:55.690: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel.  fd=190, events=0x8
01-25 23:04:56.698: WARN/InputManagerService(83): Got RemoteException sending setActive(false) notification to pid 541 uid 10004
01-25 23:04:58.078: WARN/ActivityManager(83): Launch timeout has expired, giving up wake lock!
01-25 23:04:59.405: DEBUG/dalvikvm(147): GC_EXPLICIT freed 2K, 4% free 12941K/13383K, paused 76ms+28ms
01-25 23:07:14.329: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol
01-25 23:08:58.401: DEBUG/dalvikvm(83): GC_CONCURRENT freed 1467K, 15% free 12265K/14279K, paused 28ms+36ms
01-25 23:12:14.398: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol

2 个答案:

答案 0 :(得分:3)

你有两个'创建'语句。一个包含

_id integer primary key autoincrement

根据文档是正确的。另一个

recipe_ID integer primary key not null autoincrement

我怀疑额外的“非空”是问题所在。因为它是主键,所以它本身应该是'not null',所以这可能是你的语法错误。

您还可以使用调试器找出导致问题的语句执行。围绕每个sql语句执行的try / catch块也可能是一个好主意。

答案 1 :(得分:0)

没有必要使用自动增量主键添加(非null),可以只使用/添加下面的声明。请注意,一旦将字段声明为自动增量,就不必在该字段中插入任​​何值。它将是非null和增量值。

INTEGER PRIMARY KEY AUTOINCREMENT

有关详细信息,请查看以下链接: http://www.sqlite.org/faq.html#q1