Frage

Auch wenn in dieser Anwendung keine Fehler vorliegen, stürzt sie jedes Mal ab, wenn Vactivity aufgerufen wird.Das LogCat zeigt an, dass die Tabelle Analysis_for_Today keine Spalte mit dem Namen Activated enthält.Bitte helfen Sie mir herauszufinden, was ich hier falsch gemacht habe.

MainActivity-Klasse (zuerst gestartet):

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-Klasse (gestartet, wenn MainActivity beendet wird):

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-Klasse:

public class PassValues {

String a;
String b;
int c;

public PassValues(String onValue, String offValue, int no)
{
    this.a=onValue;
    this.b=offValue;
    this.c=no;
}


public String get_onValue()
{
    return this.a;
}

public void set_onValue(String onValue)
{
    this.a=onValue;
}

public String get_offValue()
{
    return this.b;
}

public void set_offValue(String offValue)
{
    this.b=offValue;
}

public int get_count()
{
    return this.c;
}

public void set_count(int no)
{
    this.c=no;
}

}

DataBaseHandler-Klasse (verwaltet die Datenbank):

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

}

}

Teil des 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 (?,?,?,?)
War es hilfreich?

Lösung

Ihr Problem liegt hier in Ihrem CREATE TABLE SQL Command

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

Korrigieren Sie Ihre CREATE TABLE SQL Command mit unten

 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "("
        + ID + " INTEGER PRIMARY KEY," + ON + " TEXT,"
        + OFF + " TEXT," + COUNT + " INTEGER" +")";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top