嘿,我的SQLite数据库有问题,当我关闭我的应用程序然后我尝试保存我的值时,它在我的logcat中给了我一个错误。
应用程序应该在我使用sharedPreferences关闭应用程序时存储数据,当我重新打开应用程序应该从我的主类发送数据时,当我按下停止按钮时,数据应该已保存到我的SQLite数据库
为什么这不可能?
可以在不关闭应用的情况下执行此操作。
感谢您的帮助。
我的主要课程:
public class Main extends Activity{
Button bStart, bStop;
TextView tvView;
Spinner spinner1;
boolean stopValue;
int startkilometer;
String date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bStart = (Button) findViewById(R.id.bStart);
tvView = (TextView) findViewById(R.id.tvView);
spinner1 = (Spinner) findViewById(R.id.spinner1);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
stopValue = pref.getBoolean("stopper", false);
startkilometer = pref.getInt("startkm", 0);
date = pref.getString("datoen", "");
date = getIntent().getStringExtra("datoen");
startkilometer = getIntent().getIntExtra("startkm", startkilometer);
stopValue = getIntent().getBooleanExtra("stopper", stopValue);
if(stopValue == false){
bStart.setText("Start");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent start = new Intent("com.uniqueapps.runner.START");
startActivity(start);
}
});
}
if(stopValue){
bStart.setText("Stop");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent stop = new Intent("com.uniqueapps.runner.STOP");
stop.putExtra("startkm", startkilometer);
stop.putExtra("datoen", date);
startActivity(stop);
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("stopper", stopValue);
editor.putInt("startkm", startkilometer);
editor.putString("datoen", date);
editor.commit();
super.onPause();
}
我的停课:
public class Stop extends Main implements OnClickListener {
Button bStop;
EditText sqllocations, kilometer;
int startkilometer, slutkilometer, kortekm;
String locations;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.stop);
bStop = (Button) findViewById(R.id.bstopTur);
bStop.setOnClickListener(this);
sqllocations = (EditText) findViewById(R.id.locations);
kilometer = (EditText) findViewById(R.id.drovenKilometer);
startkilometer = getIntent().getIntExtra("startkm", 0);
date = getIntent().getStringExtra("datoen");
sqllocations.setText("Unknown");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bstopTur:
String locations = sqllocations.getText().toString();
slutkilometer = Integer.valueOf(kilometer.getText().toString());
int kortekm = (slutkilometer - startkilometer);
try{
KilometerSQL entry = new KilometerSQL(this);
entry.open();
entry.createEntry(date, kortekm, locations); ----> Line 60
entry.close();
}catch(Exception e){
e.printStackTrace();
}
Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("stopper", false);
startActivity(menu);
break;
}
}
}
我的KilometerSQL:
public class KilometerSQL {
public static final String KEY_ROWID = "date";
public static final String KEY_KILOMETER = "kilometer";
public static final String KEY_LOCATIONS = "locations";
private static final String DATABASE_NAME = "Kilometerdb";
private static final String DATABASE_TABLE = "kilometertable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " TEXT NOT NULL, " +
KEY_KILOMETER + " INTEGER, " +
KEY_LOCATIONS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public KilometerSQL (Context c){
ourContext = c;
}
public KilometerSQL open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String date, int kortekm, String locations) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ROWID, date);
cv.put(KEY_KILOMETER, kortekm);
cv.put(KEY_LOCATIONS, locations);
return ourDatabase.insert(DATABASE_TABLE, null, cv); ----> Line 74
}
public String getData() {
// TODO Auto-generated method stub
String [] columns = new String []{ KEY_ROWID, KEY_KILOMETER, KEY_LOCATIONS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iDate = c.getColumnIndex(KEY_ROWID);
int iKilometer = c.getColumnIndex(KEY_KILOMETER);
int iLocations = c.getColumnIndex(KEY_LOCATIONS);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result = result + c.getString(iDate) + " " + c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
}
return result;
}
}
我的Logcat:
10-01 13:54:25.865: E/SQLiteDatabase(3199): Error inserting kilometer=30 date=null locations=Unknown
10-01 13:54:25.865: E/SQLiteDatabase(3199): android.database.sqlite.SQLiteConstraintException: kilometertable.date may not be NULL (code 19)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at com.uniqueapps.runner.KilometerSQL.createEntry(KilometerSQL.java:74)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at com.uniqueapps.runner.Stop.onClick(Stop.java:60)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.view.View.performClick(View.java:4240)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.view.View$PerformClick.run(View.java:17721)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.os.Handler.handleCallback(Handler.java:730)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.os.Looper.loop(Looper.java:137)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at java.lang.reflect.Method.invoke(Method.java:525)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 13:54:25.865: E/SQLiteDatabase(3199): at dalvik.system.NativeStart.main(Native Method)