這是TeacherLoginPage.java它oncreate通過從數據庫中獲取數據來設置佈局。在onCreate方法中,我已經分配了檢查的ID使用setid()方法的方框。 這裏的問題是當我點擊應用程序強制關閉的更新按鈕時。 我認爲行cbArray.add((CheckBox) findViewById(resId));
不起作用。 我應該寫作(R.id.resId)嗎?應用程序關閉錯誤java.lang.ClassCastException:android.widget.TableRow
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
感謝您的反饋! 我做了更改,但現在它顯示了第125行的另一個錯誤,例如 空指針異常在行: cvalue [j] = toNumericalValue(cbArray.get(j).isChecked()); – Shubh
任何人都可以告訴我如何解決這個問題? – Shubh
你還沒有初始化你的數組int [] cvalue = null; – sandy