English 中文(简体)
可惜我的机器人应用程序被停了
原标题:Unfortunately my android app is stopped

After running the code below my apps donot respond. Code seems correct to me but can t understand what is wrong going on.

package com.navigationsystem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.SensorManager;
import android.util.Log;

public class DBAdapter {

    public static final String NODE_ID="nodeID";
    public static final String NODE_NAME="nodeName";
    public static final String VALUE1="value1";
    public static final String EDGE_ID="edgeID";
    public static final String TOTAL ="total";
    public static final String SOURCE_NODE_ID ="sourceNodeID";
    public static final String DESTINATION_NODE_ID ="destinationNodeID";

    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "Graph";
    private static final String DATABASE_TABLE1 = "nodes";
    private static final String DATABASE_TABLE2 = "edges";
    private static final int DATABASE_VERSION = 1 ;

    private static final String DATABASE_CREATE ="create table nodes ( " 
        +"nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL," 
        +"nodeName varchar (20) NOT NULL," 
        +"value1 int NULL," 
        +"edgeID int NULL," 
        +"total int NOT NULL );"

        +"create table edges ( edgeID int PRIMARY KEY AUTO_INCREMENT," 
        +"sourceNodeID int NOT NULL ," 
        +"destinationNodeID int NOT NULL ," 
        +"value1 int NOT NULL );";


    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

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

    private 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);
        }
        @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 titles");
            onCreate(db);
        }
    }

    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }


    public void close()
    {
        DBHelper.close();
    }

    public long addEdges(String nodeName,String value1,String edgeID) throws SQLException
    {

        ContentValues cv = new ContentValues();
        cv.put(NODE_NAME, nodeName);
        cv.put(VALUE1, value1);
        cv.put(EDGE_ID, edgeID);
        return db.insert(DATABASE_TABLE1,null,cv);

    }


}

数据库调用器类为:

package com.navigationsystem;

import android.app.Activity;
import android.os.Bundle;


public class NavigationSystemActivity extends Activity
{

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        final DBAdapter db = new DBAdapter(this);
        db.open();
        db.addEdges("A","4","1");
        db.close();
    }
}

我想在数据库中存储此值, 但既不创建表格, 也不运行我的应用程序 。

请纠正我做错了什么

谢谢

编辑编辑:logcata 错误

05-25 11:56:49.583: E/AndroidRuntime(9622): FATAL EXCEPTION: main
05-25 11:56:49.583: E/AndroidRuntime(9622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navigationsystem/com.navigationsystem.NavigationSystemActivity}: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: , while compiling: create table nodes ( nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL );
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.os.Looper.loop(Looper.java:137)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at dalvik.system.NativeStart.main(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622): Caused by: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: , while compiling: create table nodes ( nodeID int PRIMARY KEY AUTO_INCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL );
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:97)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.DBAdapter.open(DBAdapter.java:112)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at com.navigationsystem.NavigationSystemActivity.onCreate(NavigationSystemActivity.java:18)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.Activity.performCreate(Activity.java:4465)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-25 11:56:49.583: E/AndroidRuntime(9622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-25 11:56:49.583: E/AndroidRuntime(9622):     ... 11 more
最佳回答

您犯了一个小语法错误 。 将 < code> AUTO_ INCROONT 更改为 < code> aUTOINCONT


Edit:

您还提到, 您的表格中只有一个正在创建。 这可能是因为您试图将两个创建语句合并为一个。 请尝试 :

private static final String DATABASE_CREATE_1 = "CREATE TABLE NODES ( ... );";
private static final String DATABASE_CREATE_2 = "CREATE TABLE EDGES ( ... );";

然后在您 onCreate 方法中,在每个字符串上调用 execSQL :

db.execSQL(DATABASE_CREATE_1);
db.execSQL(DATABASE_CREATE_2);

您需要在修改生效前清空您现有的数据库。 只有在您这样做之后, 才会再次调用 < code> onCreate

设置 - & gt; 应用程序 - & gt; 应用程序 - & gt; [应用程序名称] - & gt; 清除数据

问题回答

Use AUTOINCREMENT instead of AUTO_INCREMENT Try this query :

create table nodes ( nodeID int PRIMARY KEY AUTOINCREMENT NOT NULL,nodeName varchar (20) NOT NULL,value1 int NULL,edgeID int NULL,total int NOT NULL )

试试...

private static final String DATABASE_CREATE ="create table nodes ( " 
    +"nodeID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," 
    +"nodeName VARCHAR (20) NOT NULL," 
    +"value1 INTEGER," 
    +"edgeID INTEGER," 
    +"total INTEGER NOT NULL );"

    +"create table edges ( edgeID INTEGER PRIMARY KEY AUTOINCREMENT," 
    +"sourceNodeID INTEGER NOT NULL ," 
    +"destinationNodeID INTEGER NOT NULL ," 
    +"value1 INTEGER NOT NULL );";

您还需要在尝试使用 onCreate(...) 方法之前删除您现有的数据库(或清晰数据),因为如果数据库已经存在,您将不再使用您 SQLite OpenHelper 方法。

需要的关键词是自动加速,而不是AUTO-INCIMON。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...