该应用运行。没有错误。在您尝试运行DatabaseTableLayout.java活动之前,应用程序崩溃。
public class DatabaseTableLayout extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelayout1);
TextView t1 = (TextView) findViewById(R.id.slot1);
TextView t2 = (TextView) findViewById(R.id.slot2);
TextView t3 = (TextView) findViewById(R.id.slot3);
TextView t4 = (TextView) findViewById(R.id.slot4);
TextView t5 = (TextView) findViewById(R.id.slot5);
DBAdapter info = new DBAdapter(this);
info.open();
String c1 = info.getRowID();
String c2 = info.getPatientID();
String c3 = info.getORoom();
String c4 = info.getSurgeon();
String c5 = info.getProcedure();
info.close();
t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);
t4.setGravity(Gravity.CENTER_HORIZONTAL);
t5.setGravity(Gravity.CENTER_HORIZONTAL);
t1.setText(c1);
t2.setText(c2);
t3.setText(c3);
t4.setText(c4);
t5.setText(c5);
}
}
数据库代码:
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_PATIENTID = "title";
public static final String KEY_OROOM = "duedate";
public static final String KEY_SURGEON = "course";
public static final String KEY_PROCEDURE = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "AssignmentsDB";
private static final String DATABASE_TABLE = "assignments";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "title VARCHAR not null, duedate date, course VARCHAR, notes VARCHAR);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String patient_id, String or_number, String surgeon, String procedure)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PATIENTID, patient_id);
initialValues.put(KEY_OROOM, or_number);
initialValues.put(KEY_SURGEON, surgeon);
initialValues.put(KEY_PROCEDURE, procedure);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records---
public Cursor getAllRecords()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_PATIENTID,
KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE}, null, null, null, null, null);
}
//---retrieves a particular record---
/*public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
*/
public String getRowID() {
String[] columns = new String[]{KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE};
Cursor mCursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int xRow = mCursor.getColumnIndex(KEY_ROWID);
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){
result = result + mCursor.getString(xRow) +"\n";
}
return result;
}
public String getPatientID() {
String[] columns = new String[]{KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE};
Cursor mCursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int xPatientID = mCursor.getColumnIndex(KEY_PATIENTID);
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){
result = result + mCursor.getString(xPatientID) +"\n";
}
return result;
}
public String getORoom() {
String[] columns = new String[]{KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE};
Cursor mCursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int xORoom = mCursor.getColumnIndex(KEY_OROOM);
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){
result = result + mCursor.getString(xORoom) +"\n";
}
return result;
}
public String getSurgeon() {
String[] columns = new String[]{KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE};
Cursor mCursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int xSurgeon = mCursor.getColumnIndex(KEY_SURGEON);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){
result = result + mCursor.getString(xSurgeon) +"\n";
}
return result;
}
public String getProcedure() {
String[] columns = new String[]{KEY_ROWID,
KEY_PATIENTID, KEY_OROOM, KEY_SURGEON, KEY_PROCEDURE};
Cursor mCursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int xProcedure = mCursor.getColumnIndex(KEY_PROCEDURE);
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){
result = result + mCursor.getString(xProcedure) +"\n";
}
return result;
}
//---updates a record---
public boolean updateRecord(long rowId, String patient_id, String or_number, String surgeon, String procedure)
{
ContentValues args = new ContentValues();
args.put(KEY_PATIENTID, patient_id);
args.put(KEY_OROOM, or_number);
args.put(KEY_SURGEON, surgeon);
args.put(KEY_PROCEDURE, procedure);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
基本上,5个方法getRowID(),getPatientID(),getORoom(),getSurgeon(),getProcedure()应该获取用户输入的信息并将其显示在表中。但如前所述,当您尝试在应用程序中访问DatabaseTableLayout.java时,应用程序崩溃。
注意:数据库已经过toast测试,实际上存储了用户的输入。只是表格显示不起作用。
logcat的:
03-10 18:58:45.801: E/AndroidRuntime(959): FATAL EXCEPTION: main
03-10 18:58:45.801: E/AndroidRuntime(959): java.lang.IllegalStateException: Could not execute method of the activity
03-10 18:58:45.801: E/AndroidRuntime(959): at android.view.View$1.onClick(View.java:3633)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.view.View.performClick(View.java:4240)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.view.View$PerformClick.run(View.java:17721)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.os.Handler.handleCallback(Handler.java:730)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.os.Looper.loop(Looper.java:137)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-10 18:58:45.801: E/AndroidRuntime(959): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:58:45.801: E/AndroidRuntime(959): at java.lang.reflect.Method.invoke(Method.java:525)
03-10 18:58:45.801: E/AndroidRuntime(959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-10 18:58:45.801: E/AndroidRuntime(959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-10 18:58:45.801: E/AndroidRuntime(959): at dalvik.system.NativeStart.main(Native Method)
03-10 18:58:45.801: E/AndroidRuntime(959): Caused by: java.lang.reflect.InvocationTargetException
03-10 18:58:45.801: E/AndroidRuntime(959): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:58:45.801: E/AndroidRuntime(959): at java.lang.reflect.Method.invoke(Method.java:525)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.view.View$1.onClick(View.java:3628)
03-10 18:58:45.801: E/AndroidRuntime(959): ... 11 more
03-10 18:58:45.801: E/AndroidRuntime(959): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.SurgicalMate/com.example.SurgicalMate.DatabaseTableLayout}; have you declared this activity in your AndroidManifest.xml?
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Activity.startActivityForResult(Activity.java:3390)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Activity.startActivityForResult(Activity.java:3351)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Activity.startActivity(Activity.java:3587)
03-10 18:58:45.801: E/AndroidRuntime(959): at android.app.Activity.startActivity(Activity.java:3555)
03-10 18:58:45.801: E/AndroidRuntime(959): at com.example.SurgicalMate.NewSessionPage.goToDB(NewSessionPage.java:53)
03-10 18:58:45.801: E/AndroidRuntime(959): ... 14 more