应用程序强制关闭,错误为java.lang.ClassCastException:android.widget.TableRow

时间:2013-08-12 08:54:11

标签: android sqlite

这是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

4 个答案:

答案 0 :(得分:1)

您正在尝试将TableRow投射到CheckBox。确保ID指向CheckBox

答案 1 :(得分:1)

请注意,您之前为TableRow分配了ID,但是当您从resId检索时,您会转换为导致问题的CheckBox。检查一下:

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));

和此:

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());
        }

希望它有所帮助。

答案 2 :(得分:1)

您正在为TableRow和Checkbox分配相同的ID。如

 row.setId(i);

 checkBox.setId(i);

只需评论//row.setId(i);

答案 3 :(得分:1)

您的问题是您设置了两次ID:

row.setId(i);

以后

checkBox.setId(i);

如果您执行findViewById(resId),那么它会返回它使用此ID找到的第一个元素,即TableRow,而不是您预期的CheckBox。因此ClassCastException