2014-04-12 74 views
1

即使有這個應用程序沒有錯誤,但每個Vactivity被調用時崩潰。 LogCat顯示該表Analysis_for_Today沒有名爲Activated的列。請幫助我找到我在這裏做錯了什麼。有沒有列錯誤:SQLite的Android的

MainActivity類別(推出第一):

public class MainActivity extends Activity{ 


long currentTimeMillis; 
long endTimeMillis; 
public int count = 0; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    currentTimeMillis = System.currentTimeMillis(); 

    final MediaPlayer mp = MediaPlayer.create(this, R.drawable.beep); 
    Button b = (Button) findViewById(R.id.button1); 

    b.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v) { 
      mp.start(); 
      if(mp.isPlaying()) 
       count++; 


      } 
    }); 


} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    endTimeMillis = System.currentTimeMillis(); 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("on",String.valueOf(currentTimeMillis)); 
    editor.putString("off",String.valueOf(endTimeMillis)); 
    editor.putInt("count", count); 
    editor.commit(); 

    Intent intent =new Intent(MainActivity.this, Vactivity.class); 
    startActivity(intent); 

    } 

}

Vactivity類(啓動時MainActivity退出):

public class Vactivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String onValue = preferences.getString("on", ""); 
    String offValue = preferences.getString("off", ""); 
    int no = preferences.getInt("count", 0); 

    DataBaseHandler db = new DataBaseHandler(this); 
    db.addDetails(new PassValues(onValue, offValue, no)); 

    setContentView(R.layout.activity_v); 
} 

}

PassValues類:

}

數據庫處理器類(處理DB):

public class DataBaseHandler extends SQLiteOpenHelper { 
private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "Analysis_DB"; 
private static final String TABLE_TODAY = "Analysis_for_Today"; 

private static final String ID = "_ID"; 
private static final String ON = "Activated"; 
private static final String OFF = "Terminated"; 
private static final String COUNT = "Times_Dozed"; 


public DataBaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "(" 
      + ID + " INTEGER PRIMARY KEY," + ON + " TEXT," 
      + OFF + " TEXT," + COUNT + " INTEGER," +")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODAY); 

    // Create tables again 
    onCreate(db); 

} 


public void addDetails(PassValues p) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(ID, 1); 
    values.put(ON, p.get_onValue()); 
    values.put(OFF, p.get_offValue()); 
    values.put(COUNT, p.get_count()); 

    // Inserting Row 
    db.insert(TABLE_TODAY, null, values); 
    db.close(); // Closing database connection 

} 

}

部分的logcat的:

04-12 07:54:46.197: E/SQLiteLog(2604): (1) table Analysis_for_Today has no column named Activated 
    04-12 07:54:46.287: E/SQLiteDatabase(2604): Error inserting Activated=1397303677511 Terminated=1397303685201 Times_Dozed=1 _ID=1 
    04-12 07:54:46.287: E/SQLiteDatabase(2604): android.database.sqlite.SQLiteException: table Analysis_for_Today has no column named Activated (code 1): , while compiling: INSERT INTO Analysis_for_Today(Activated,Terminated,Times_Dozed,_ID) VALUES (?,?,?,?) 
+0

嘗試按我的解決方案,給我這個反饋。 –

回答

1

你的問題是在你這裏CREATE TABLE SQL Command

+ OFF + " TEXT," + COUNT + " INTEGER," +")"; // remove semi column after INTEGER 

糾正你CREATE TABLE SQL Command下面

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "(" 
     + ID + " INTEGER PRIMARY KEY," + ON + " TEXT," 
     + OFF + " TEXT," + COUNT + " INTEGER" +")"; 
+0

它沒有解決問題。無論如何非常感謝您的及時回覆!仍在尋找這個 –

+0

的問題,但這個改變之後,你必須卸載應用程序,並重新安裝,使您的舊數據庫中刪除,並創建新的數據庫。 –

+0

你是說從我的設備上卸載吧?我試過了。但我一直得到同樣的錯誤:( –