2016-11-20 4 views
0

エラーが発生し、ボタンをクリックするとアプリケーションがクラッシュし、5番目のアクティビティに移動します。私を助けてください。それは原因:データベースからリストビューを作成中にエラーが発生しました

java.lang.IllegalArgumentException: column '_id'私はすでにデータベース内に '_id'を作成しています。助けてください。スンダ除いて、あなたのすべてのcreate tableクエリで

FifthActivity.Java

package com.example.kiran.herau; 

public class FifthActivity extends AppCompatActivity { 
    NewDatabaseAdapter newHelper; 
    private SimpleCursorAdapter dataAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_fifth); 
     newHelper = new NewDatabaseAdapter(this); 
     displayView(); 
    } 

    private void displayView() { 

     newHelper = new NewDatabaseAdapter(this); 
     Cursor cursor = newHelper.fetchAllData(); 

     // The desired columns to be bound 
     String[] columns = new String[]{ 
       NewDatabaseAdapter.NewDatabaseHelper.SUBJECT_NAME, 
       NewDatabaseAdapter.NewDatabaseHelper.TEACHER_NAME, 
     }; 

     // the XML defined views which the data will be bound to 
     int[] to = new int[]{ 
       R.id.subject, 
       R.id.teacher, 
     }; 

     // create the adapter using the cursor pointing to the desired data 
     //as well as the layout information 
     dataAdapter = new SimpleCursorAdapter(
       this, R.layout.routinerow, 
       cursor, 
       columns, 
       to, 
       0); 

     ListView listView = (ListView) findViewById(R.id.listView); 
     // Assign adapter to ListView 
     listView.setAdapter(dataAdapter); 
    } 
} 

NewDatabaseAdapter.java

package com.example.kiran.herau; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import layout.PrimaryFragment; 

public class NewDatabaseAdapter { 

     NewDatabaseHelper datahelper; 

     public NewDatabaseAdapter(Context context) { 
      datahelper = new NewDatabaseHelper(context); 
     } 

    public long insertFirst(String subject, String teacher, String starttime, String endtime, String day) { 
      SQLiteDatabase db = datahelper.getWritableDatabase(); 
      ContentValues contentValues = new ContentValues(); 
      contentValues.put(datahelper.SUBJECT_NAME, subject); 
      contentValues.put(datahelper.TEACHER_NAME, teacher); 
      contentValues.put(datahelper.START_TIME, starttime); 
      contentValues.put(datahelper.END_TIME, endtime); 


      long id = db.insert(day, null, contentValues); 

      return id; 
     } 

    public Cursor fetchAllData() { 
     SQLiteDatabase db = datahelper.getWritableDatabase(); 
     Cursor mCursor = db.query(NewDatabaseHelper.SUNDAY_TABLE_NAME, new String[] {NewDatabaseHelper.SUBJECT_NAME, NewDatabaseHelper.TEACHER_NAME}, 
       null, null, null, null, null); 

     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    static class NewDatabaseHelper extends SQLiteOpenHelper { 


     public static final String UID = "_id"; 
      public static final String DATABASE_NAME = "mydatabase"; 
      public static final String SUNDAY_TABLE_NAME = "Sunday"; 
      public static final String MONDAY_TABLE_NAME = "Monday"; 
      public static final String TUESDAY_TABLE_NAME = "Tuesday"; 
      public static final String WEDNESDAY_TABLE_NAME = "Wednesday"; 
      public static final String THURSDAY_TABLE_NAME = "Thursday"; 
      public static final String FRIDAY_TABLE_NAME = "Friday"; 
      public static final String SATURDAY_TABLE_NAME = "Saturday"; 
      public static final int DATABASE_VERSION = 2; 
      public static final String SUBJECT_NAME = "SubjectName"; 
      public static final String TEACHER_NAME = "TeacherName"; 
      public static final String START_TIME= "StartTime"; 
      public static final String END_TIME= "EndTime"; 

      public static final String CREATE_SUNDAY_TABLE = 

      "CREATE TABLE " + SUNDAY_TABLE_NAME + " (" + 
        UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
          SUBJECT_NAME + " VARCHAR(255)," + 
        TEACHER_NAME + " VARCHAR(255)," + 
      START_TIME + " VARCHAR(255)," + 

      END_TIME + " VARCHAR(255)" + 
        ");"; 

      public static final String CREATE_MONDAY_TABLE = 

        "CREATE TABLE " + MONDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 

      public static final String CREATE_TUESDAY_TABLE = 

        "CREATE TABLE " + TUESDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 


      public static final String CREATE_WEDNESDAY_TABLE = 

        "CREATE TABLE " + WEDNESDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 


      public static final String CREATE_THURSDAY_TABLE = 

        "CREATE TABLE " + THURSDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 


      public static final String CREATE_FRIDAY_TABLE = 

        "CREATE TABLE " + FRIDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 

      public static final String CREATE_SATURDAY_TABLE = 

        "CREATE TABLE " + SATURDAY_TABLE_NAME + " (" + 
          SUBJECT_NAME + " VARCHAR(255)," + 
          TEACHER_NAME + " VARCHAR(255)," + 
          START_TIME + " VARCHAR(255)," + 

          END_TIME + " VARCHAR(255)" + 
          ");"; 



      private static final String DROP_SUNDAY_TABLE = "DROP TABLE IF EXISTS " + SUNDAY_TABLE_NAME; 
      private static final String DROP_MONDAY_TABLE = "DROP TABLE IF EXISTS " + MONDAY_TABLE_NAME; 
      private static final String DROP_TUESDAY_TABLE = "DROP TABLE IF EXISTS " + TUESDAY_TABLE_NAME; 
      private static final String DROP_WEDNESDAY_TABLE = "DROP TABLE IF EXISTS " + WEDNESDAY_TABLE_NAME; 
      private static final String DROP_THURSDAY_TABLE = "DROP TABLE IF EXISTS " + THURSDAY_TABLE_NAME; 
      private static final String DROP_FRIDAY_TABLE = "DROP TABLE IF EXISTS " + FRIDAY_TABLE_NAME; 
      private static final String DROP_SATURDAY_TABLE = "DROP TABLE IF EXISTS " + SATURDAY_TABLE_NAME; 

      private Context context; 
      public NewDatabaseHelper(Context context) { 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
       this.context = context; 
       Message.message(context, "Constructor CAlled"); 
      } 


      @Override 
      public void onCreate(SQLiteDatabase db) { 
       try { 
        Message.message(context, " ONCREATE CALEED"); 
        db.execSQL(CREATE_SUNDAY_TABLE); 
        Message.message(context, " FIRST TABLE CREATED"); 
        db.execSQL(CREATE_MONDAY_TABLE); 
        Message.message(context, " SECOND TABLE CREATED"); 
        db.execSQL(CREATE_TUESDAY_TABLE); 
        db.execSQL(CREATE_WEDNESDAY_TABLE); 
        db.execSQL(CREATE_THURSDAY_TABLE); 
        db.execSQL(CREATE_FRIDAY_TABLE); 
        db.execSQL(CREATE_SATURDAY_TABLE); 
       } catch (Exception e) { 
        Message.message(context, "" + e); 

       } 

      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
       try { 
        Message.message(context, "OnUpdrage Caleld"); 
        db.execSQL(DROP_SUNDAY_TABLE); 
        Message.message(context, "First Table Dropped"); 
        db.execSQL(DROP_MONDAY_TABLE); 
        Message.message(context, "Second Table Dropped"); 
        db.execSQL(DROP_TUESDAY_TABLE); 
        db.execSQL(DROP_WEDNESDAY_TABLE); 
        db.execSQL(DROP_THURSDAY_TABLE); 
        db.execSQL(DROP_FRIDAY_TABLE); 
        db.execSQL(DROP_SATURDAY_TABLE); 
        onCreate(db); 
       } catch (Exception e) { 
        Message.message(context, "" + e); 

       } 
      } 
     } 
    } 

LogCat

11-20 04:20:41.633 1107-1107/com.example.kiran.herau E/AndroidRuntime: FATAL EXCEPTION: main 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kiran.herau/com.example.kiran.herau.FifthActivity}: java.lang.IllegalArgumentException: column '_id' does not exist 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                      at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:137) 
                      at android.app.ActivityThread.main(ActivityThread.java:4745) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:511) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                      at dalvik.system.NativeStart.main(Native Method) 
                     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist 
                      at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302) 
                      at android.widget.CursorAdapter.init(CursorAdapter.java:168) 
                      at android.widget.CursorAdapter.<init>(CursorAdapter.java:145) 
                      at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91) 
                      at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104) 
                      at com.example.kiran.herau.FifthActivity.displayView(FifthActivity.java:43) 
                      at com.example.kiran.herau.FifthActivity.onCreate(FifthActivity.java:18) 
                      at android.app.Activity.performCreate(Activity.java:5008) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)  
                      at android.app.ActivityThread.access$600(ActivityThread.java:130)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)  
                      at android.os.Handler.dispatchMessage(Handler.java:99)  
                      at android.os.Looper.loop(Looper.java:137)  
                      at android.app.ActivityThread.main(ActivityThread.java:4745)  
                      at java.lang.reflect.Method.invokeNative(Native Method)  
                      at java.lang.reflect.Method.invoke(Method.java:511)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  
                      at dalvik.system.NativeStart.main(Native Method)  
11-20 04:20:43.193 1107-1107/com.example.kiran.herau I/Process: Sending signal. PID: 1107 SIG: 9 

答えて

0

yは、テーブルを作成しながら、ここでもUID "_id"を追加するだけで、4つの列

SUBJECT_NAME + " VARCHAR(255)," + 
TEACHER_NAME + " VARCHAR(255)," + 
START_TIME + " VARCHAR(255)," + 
END_TIME + " VARCHAR(255) " 

があります。私が見つけたもうひとつ

(_id INNTEGER PRIMARY KEY AUTOINCREMENT... 

よう (_idの間で単一のスペースを与えることをお勧めし

"CREATE TABLE " + SUNDAY_TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUBJECT_NAME + " VARCHAR(255)," + TEACHER_NAME + " VARCHAR(255)," + START_TIME + " VARCHAR(255)," + END_TIME + " VARCHAR(255)" + ");"; 

CursorAdapterは、それはdoesnotデフォルト

// The desired columns to be bound 
String[] columns = new String[]{ 
    NewDatabaseAdapter.NewDatabaseHelper.UID, //add this column as well 
    NewDatabaseAdapter.NewDatabaseHelper.SUBJECT_NAME, 
    NewDatabaseAdapter.NewDatabaseHelper.TEACHER_NAME, 
}; 
+0

によって_id列が必要です作業。エラーが何度も繰り返されます。 – Kiran

+0

同じエラー?週テーブル –

+0

のすべての日のテーブル作成クエリを変更して、テーブルが再作成されていることを確認してください – Raiv

関連する問題