这是TeacherLoginPage.java,oncreate 通过从数据库中获取数据来设置布局。在onCreate 方法中,我使用setid() 方法分配了复选框的ID。这里的问题是当我单击更新按钮时,应用程序强制关闭。我认为这条线cbArray.add((CheckBox) findViewById(resId));
是行不通的。我应该写成(R.id.resId)吗?
TeacherLoginPage.java
LinearLayout my_layout = (LinearLayout) findViewById(R.id.layout_checklist);
for (int i = 0; i < Array_Count; i++) {
TableRow row = new TableRow(this);
row.setId(i);
row.setLayoutParams(new LayoutParams(
android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
CheckBox checkBox = new CheckBox(this);
checkBox.setId(i);
checkBox.setText(Roll_array.get(i));
row.addView(checkBox);
my_layout.addView(row);
}
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<CheckBox> cbArray = new ArrayList<CheckBox>();
int[] cvalue = null;
for (int j = 0; j < Array_Count; j++) {
int resId = getResources().getIdentifier(String.valueOf(j), "id", getPackageName());
cbArray.add((CheckBox) findViewById(resId));
cvalue[j] = toNumericalValue(cbArray.get(j).isChecked());
}
insertAttendanceDatabase(TeacherLoggedInPage.this, cvalue);
}
private void insertAttendanceDatabase(
TeacherLoggedInPage teacherLoggedInPage, int[] cvalue) {
// TODO Auto-generated method stub
SQLiteDatabase db = myDb.getWritableDatabase();
int length = cvalue.length;
ContentValues cv = new ContentValues();
cv.put("teacher_id", t_id);
cv.put("time", DateFormat.getDateTimeInstance().format(new Date()));
for (int i = 0; i < cvalue.length; i++) {
cv.put(Roll_array.get(i), cvalue[i]);
}
try {
db.insert(DbHelper.ATTENDANCE_TABLE, null, cv);
} catch (Exception e) {
e.printStackTrace();
}
}
private int toNumericalValue(boolean checked) {
// TODO Auto-generated method stub
if (checked)
return 1;
else
return 0;
}
这是LogCat
08-12 04:17:14.970: D/AndroidRuntime(385): Shutting down VM
08-12 04:17:14.999: W/dalvikvm(385): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-12 04:17:15.009: E/AndroidRuntime(385): FATAL EXCEPTION: main
08-12 04:17:15.009: E/AndroidRuntime(385): java.lang.ClassCastException: android.widget.TableRow
08-12 04:17:15.009: E/AndroidRuntime(385): at com.example.attendancesystem.TeacherLoggedInPage.onClick(TeacherLoggedInPage.java:123)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.view.View.performClick(View.java:2408)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.view.View$PerformClick.run(View.java:8816)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.os.Handler.handleCallback(Handler.java:587)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.os.Looper.loop(Looper.java:123)
08-12 04:17:15.009: E/AndroidRuntime(385): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 04:17:15.009: E/AndroidRuntime(385): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 04:17:15.009: E/AndroidRuntime(385): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 04:17:15.009: E/AndroidRuntime(385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-12 04:17:15.009: E/AndroidRuntime(385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-12 04:17:15.009: E/AndroidRuntime(385): at dalvik.system.NativeStart.main(Native Method)
08-12 04:17:38.108: I/Process(385): Sending signal. PID: 385 SIG: 9