Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 使用ArrayAdapter强制关闭ListActivity_Sqlite_Android Activity_Listactivity_Sqliteopenhelper - Fatal编程技术网

Sqlite 使用ArrayAdapter强制关闭ListActivity

Sqlite 使用ArrayAdapter强制关闭ListActivity,sqlite,android-activity,listactivity,sqliteopenhelper,Sqlite,Android Activity,Listactivity,Sqliteopenhelper,我正在尝试使用这个站点上的Android SQLite数据库制作一个示例: 一切都会好起来的。但是,当我使用简单的活动时,这很好。对于ListActivity,我有强制关闭错误!不知道该怎么办。请帮忙 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_

我正在尝试使用这个站点上的Android SQLite数据库制作一个示例: 一切都会好起来的。但是,当我使用简单的活动时,这很好。对于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"
   />
您应该正确拼写
autoincrement
。 列名及其数据类型(_idinteger)和(注释文本)之间还需要一个空格。

解决方案:-

  • 更新你的应用程序
  • 清除Gmail存储数据
  • 清除应用缓存和数据
  • 重置应用程序首选项
  • 重置智能手机出厂设置
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
near "autoincreament": syntax error: create table comments(_idinteger primary key autoincreament, commenttext not null);