使用ArrayAdapter强制关闭ListActivity

时间:2013-10-22 12:30:03

标签: sqlite android-activity listactivity sqliteopenhelper

我正在尝试使用此站点中的Android SQLite数据库进行示例: http://www.vogella.com/articles/AndroidSQLite/article.html 一切都可能是好的。但是,当我使用简单的活动时它很好。使用ListActivity我有强制关闭错误!不知道该怎么办。请帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add New" 
        android:onClick="onClick"/>

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete First" 
        android:onClick="onClick"/>


<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   />

public class MainActivity extends ListActivity {

 private CommentDataSourse datasource;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_two);

    datasource = new CommentDataSourse(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    // use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  // Will be called via the onClick attribute
  // of the buttons in main.xml
 public void onClick(View view) {
    @SuppressWarnings("unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    Comment comment = null;
    switch (view.getId()) {
    case R.id.add:
      String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
      int nextInt = new Random().nextInt(3);
      // save the new comment to the database
      comment = datasource.createComment(comments[nextInt]);
      adapter.add(comment);
      break;
    case R.id.delete:
      if (getListAdapter().getCount() > 0) {
        comment = (Comment) getListAdapter().getItem(0);
        datasource.deleteComment(comment);
        adapter.remove(comment);
      }
      break;
    }
    adapter.notifyDataSetChanged();
  }

  @Override
  protected void onResume() {
    datasource.open();
    super.onResume();
  }

  @Override
  protected void onPause() {
    datasource.close();
    super.onPause();
  }

}

日志文件:

10-22 12:14:49.794: I/Database(11578): sqlite returned: error code = 1, msg = near "autoincreament": syntax error
10-22 12:14:49.794: E/Database(11578): Failure 1 (near "autoincreament": syntax error)   on 0x2aa3f8 when preparing 'create table comments(_idinteger primary key autoincreament,   commenttext not null);'.
10-22 12:14:49.804: D/AndroidRuntime(11578): Shutting down VM
10-22 12:14:49.804: W/dalvikvm(11578): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-22 12:14:49.844: E/AndroidRuntime(11578): FATAL EXCEPTION: main
10-22 12:14:49.844: E/AndroidRuntime(11578): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testmysdkdb/com.example.testmysdkdb.MainActivity}: android.database.sqlite.SQLiteException: near "autoincreament": syntax error: create table comments(_idinteger primary key autoincreament, commenttext not null);
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.os.Looper.loop(Looper.java:123)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread.main(ActivityThread.java:3683)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at java.lang.reflect.Method.invoke(Method.java:507)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at dalvik.system.NativeStart.main(Native Method) 
10-22 12:14:49.844: E/AndroidRuntime(11578): Caused by: android.database.sqlite.SQLiteException: near "autoincreament": syntax error: create table comments(_idinteger primary key autoincreament, commenttext not null);
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at com.example.testmysdkdb.MyDBHelper.onCreate(MyDBHelper.java:21)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at com.example.testmysdkdb.CommentDataSourse.open(CommentDataSourse.java:23)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at com.example.testmysdkdb.MainActivity.onCreate(MainActivity.java:22)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 12:14:49.844: E/AndroidRuntime(11578):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-22 12:14:49.844: E/AndroidRuntime(11578):    ... 11 more

2 个答案:

答案 0 :(得分:0)

near "autoincreament": syntax error: create table comments(_idinteger primary key autoincreament, commenttext not null);

你应该正确拼写autoincrement。 您还需要在列名称及其数据类型(_id整数)和(注释文本)之间留一个空格。

答案 1 :(得分:0)

解决方案: -

  • 更新你的应用
  • 清除Gmail存储空间数据
  • 清除应用缓存和数据
  • 重置应用偏好设置
  • 重置智能手机出厂设置
相关问题