1

我正在尝试运行一个类,该类将检查数据库中的信息,然后将其显示在屏幕上。它昨天还在工作,直到我为它添加代码以检查第二个表中的信息我将同时发布代码和 logcat,因为我不知道为什么会这样

public class WorkoutProgress extends ListActivity {

  private DataBaseHelper datasource;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    /*requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress);

    datasource = new DataBaseHelper(this);
    datasource.open();
    fillData();
    datasource.close();
  }
  private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor c = datasource.getAllTitles();
    startManagingCursor(c);

    String[] from = new String[] {DataBaseHelper.KEY_CODE,  DataBaseHelper.KEY_DAYS,DataBaseHelper.KEY_BMI };
    int[] to = { R.id.code, R.id.Days, R.id.BMI };
    String[] from2 = new String[] {DataBaseHelper.KEY_DATE,  DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs };
    int[] to2 = { R.id.date, R.id.steps, R.id.cals };
    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes =
     new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
    setListAdapter(notes);
    SimpleCursorAdapter notes2 =
      new SimpleCursorAdapter(this, R.layout.notes_row, c, from2, to2);
    setListAdapter(notes);
  }

  public void add(View view) {
    //Do nothing
  }

  public void delete(View view) {
     datasource.open();
     datasource.deleteFirst();
     fillData();
     datasource.close();
  }
}

当我尝试访问此类时,日志猫是

04-06 01:10:42.293: E/global(22219): java.lang.UnsupportedOperationException
04-06 01:10:42.293: E/global(22219):    at java.lang.VMThread.stop(VMThread.java:85)
04-06 01:10:42.293: E/global(22219):    at java.lang.Thread.stop(Thread.java:1391)
04-06 01:10:42.293: E/global(22219):    at java.lang.Thread.stop(Thread.java:1356)
04-06 01:10:42.293: E/global(22219):    at com.b00348312.workout.Splashscreen$1.run(Splashscreen.java:42)
04-06 01:10:45.393: I/Database(22219): sqlite returned: error code = 1, msg = AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
04-06 01:10:45.393: E/Database(22219): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x2c0e38 when preparing 'create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null);'.
04-06 01:10:45.403: D/AndroidRuntime(22219): Shutting down VM
04-06 01:10:45.403: W/dalvikvm(22219): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
04-06 01:10:45.453: D/dalvikvm(22219): GC_FOR_MALLOC freed 4012 objects / 253248 bytes in 32ms
04-06 01:10:45.453: E/AndroidRuntime(22219): FATAL EXCEPTION: main
04-06 01:10:45.453: E/AndroidRuntime(22219): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null);
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.os.Looper.loop(Looper.java:144)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread.main(ActivityThread.java:4937)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at java.lang.reflect.Method.invoke(Method.java:521)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at dalvik.system.NativeStart.main(Native Method)
04-06 01:10:45.453: E/AndroidRuntime(22219): Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null);
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1817)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at com.b00348312.workout.DataBaseHelper$DatabaseHelper.onCreate(DataBaseHelper.java:59)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at com.b00348312.workout.DataBaseHelper.open(DataBaseHelper.java:78)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:21)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
04-06 01:10:45.453: E/AndroidRuntime(22219):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
04-06 01:10:45.453: E/AndroidRuntime(22219):    ... 11 more
4

3 回答 3

3

堆栈跟踪说:

准备“创建表目标(_id 主键自动增量,代码文本不为空,天标题文本不为空,bmi 文本不为空);”时在 0x2c0e38 上失败 1(仅允许在整数主键上使用自动增量)。

看起来您忘记在语句中将该_id字段声明为 an 。INTEGERcreate table

于 2012-04-06T00:22:36.713 回答
0

您的 DataBaseHelper 可能没有_ID在创建数据库时指定列的类型。您必须将其定义为INTEGER.

于 2012-04-06T00:25:41.413 回答
0

我刚刚开始 Android 编程,我使用 USB 调试来试用我的应用程序。我意识到在可能的 SQL 失败后,下次运行数据库时不会从您的设备中删除数据库,并且您所做的任何更改都不会生效。

所以说你的“创建表”SQL语句在某种程度上是错误的。你的程序崩溃了。你找到问题并解决它。如果您再次运行程序,除非您增加版本,否则将使用相同的旧数据库。

因此,请转到您的设备并卸载该程序,然后从您的计算机再次运行它。这一次,我认为将使用您新的“创建表”SQL 重新创建数据库。

Since I'm new, this might not be what is happening and I'm open to correction if that's so.

于 2012-04-26T20:12:02.663 回答