我是android编程的新手。我已经在单独的 java 类中创建了一个数据库,并且我有一个活动,我想要一些数据,尤其是来自 EditText 的数据,以便在单击按钮时存储到数据库中。我在代码中做错了什么?在 OnClick 事件中包含数据库代码后,它不会启动我的 savecount 活动。
当 SaveClass 活动启动时,它会冻结应用程序,并且 Eclipse 会显示以下消息:“未找到源”,并带有编辑源查找路径的选项。
Logcat 以递归方式显示以下内容,直到启动超时:
08-08 18:06:37.313: D/dalvikvm(1676): threadid=1: still suspended after undo (sc=1 dc=1)
08-08 18:06:37.323: I/dalvikvm(1676): Wrote stack traces to '/data/anr/traces.txt'
08-08 18:06:37.793: I/Process(896): Sending signal. PID: 1676 SIG: 3
08-08 18:06:37.793: I/dalvikvm(1676): threadid=3: reacting to signal 3
08-08 18:06:37.823: D/dalvikvm(1676): threadid=1: still suspended after undo (sc=1 dc=1)
08-08 18:06:37.823: I/dalvikvm(1676): Wrote stack traces to '/data/anr/traces.txt'
08-08 18:06:38.042: W/ActivityManager(896): Launch timeout has expired, giving up wake lock!
08-08 18:06:38.175: W/ActivityManager(896): Activity idle timeout for ActivityRecord{b4bf2e18 com.simple.tally.counter/.SaveCount}
类dbOpenHelper
package com.simple.tally.counter;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class classdbOpenHelper extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_COUNTED = "countedno";
public static final String KEY_DESC = "countdesc";
public static final String KEY_DATE = "countdate";
public static final String DATABASE_NAME= "countdb";
public static final String DATABASE_TABLE = "countable";
public static final int DATABASE_VERSION = 1;
public classdbOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGET PRIMARY KEY AUTOINCREMENT " +
KEY_COUNTED + " INTEGER " +
KEY_DESC + " TEXT NOT NULL " +
KEY_DATE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
onCreate(db);
}
}
SaveCount.java
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
public class SaveCount extends Activity {
TextView metersave;
classdbOpenHelper eventsData;
EditText desc;
Button Save;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_count);
Intent launchNewIntent = getIntent();
String count = launchNewIntent.getStringExtra(SimpleTallyCounter.EXTRA_MESSAGE);
metersave = (TextView) findViewById(R.id.metersave);
metersave.setText(count);
final Button Save = (Button) findViewById(R.id.Save);
Save.setOnClickListener(new clicker());
eventsData = new classdbOpenHelper(this);
desc = (EditText) findViewById(R.id.desc);
}
@Override
public void onDestroy() {
eventsData.close();
}
class clicker implements Button.OnClickListener {
public void onClick(View v) {
String descvalue = desc.getText().toString();
uSQLiteDatabase db = eventsData.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(classdbOpenHelper.KEY_COUNTED, metersave.getText().toString());
cv.put(classdbOpenHelper.KEY_DESC, descvalue);
cv.put(classdbOpenHelper.KEY_DATE, System.currentTimeMillis());
db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
}
}
}