引发ForceClose的TextView的ClassCastException

时间:2013-12-23 09:02:47

标签: android android-layout android-linearlayout

我正在设计一个以RelativeLayout为根的布局,后面跟着'n'个子和一个LinearLayout。在LinearLayout内部我有一个TextView,它会导致一个力关闭。请查看我的代码并帮助我:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="true" >

    <include
        android:id="@+id/include1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        layout="@layout/ksh_header_logo" >
    </include>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/include1"
        android:background="@color/Gray"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_lbl_add_bill_and_expense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:text="@string/add"
            android:textColor="@color/white"
            android:textSize="15sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/txt_lbl_add_bill_and_expense_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:text="@string/bill_and_expense"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txt_lbl_add_bill_and_expense_for"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:text="@string/FOR"
            android:textColor="@color/white"
            android:textSize="15sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/txt_lbl_add_bill_and_expense_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:singleLine="true"
            android:text=""
            android:textColor="@color/white"
            android:textSize="15sp"
            android:textStyle="normal" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:background="@color/ksh_login" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="3dp"
            android:background="@color/white" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="10dp"
                    android:drawableRight="@drawable/mandatory"
                    android:text="@string/date_of_expense"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_add_bill_and_expense_date"
                    android:layout_width="150dp"
                    android:layout_height="40dp"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/blue_button"
                    android:singleLine="true" />

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_type"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_below="@+id/btn_add_bill_and_expense_date"
                    android:layout_marginTop="10dp"
                    android:drawableRight="@drawable/mandatory"
                    android:text="@string/expense_type"
                    android:textSize="15sp" />

                <AutoCompleteTextView
                    android:id="@+id/edit_add_bill_and_expense_type"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_type"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_type"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/edit_text_border"
                    android:ems="10"
                    android:inputType="text" />

                <CheckBox
                    android:id="@+id/chk_bill_expense_it_eligible"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/edit_add_bill_and_expense_type"
                    android:layout_marginTop="8dp"
                    android:text="@string/it_section_eligible"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_vendor_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/edit_add_bill_and_expense_type"
                    android:layout_below="@+id/chk_bill_expense_it_eligible"
                    android:drawableRight="@drawable/mandatory"
                    android:text="@string/vendor_name"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/edit_add_bill_and_expense_vendor_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_vendor_name"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_vendor_name"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/edit_text_border"
                    android:ems="10"
                    android:inputType="text" />

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_bill_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/edit_add_bill_and_expense_vendor_name"
                    android:layout_below="@+id/edit_add_bill_and_expense_vendor_name"
                    android:drawableRight="@drawable/mandatory"
                    android:text="@string/bill_number"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/edit_add_bill_and_expense_bill_number"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_bill_number"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_bill_number"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/edit_text_border"
                    android:ems="10"
                    android:inputType="number" />

                <LinearLayout
                    android:id="@+id/linear_sub_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_below="@+id/edit_add_bill_and_expense_bill_number"
                    android:layout_marginTop="5dp"
                    android:orientation="horizontal"
                    android:weightSum="5" >

                    <LinearLayout
                        android:id="@+id/linear_sub_inner_layout_1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="3"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_lbl_add_bill_and_expense_amount_spent"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:drawableRight="@drawable/mandatory"
                            android:text="@string/amount_spent"
                            android:textSize="15sp" />

                        <EditText
                            android:id="@+id/edit_add_bill_and_expense_amount_spent"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_border"
                            android:ems="10"
                            android:inputType="numberDecimal"
                            android:maxLength="10" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linear_sub_inner_layout_2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_lbl_add_bill_and_expense_currency_code"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:drawableRight="@drawable/mandatory"
                            android:text="@string/currency_code"
                            android:textSize="15sp" />

                        <EditText
                            android:id="@+id/edit_add_bill_and_expense_currency_code"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_border"
                            android:ems="10"
                            android:inputType="textCapCharacters"
                            android:maxLength="3"
                            android:paddingLeft="5dp"
                            android:text="@string/currency_code_inr" />
                    </LinearLayout>
                </LinearLayout>

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_choose_file"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/linear_sub_layout"
                    android:layout_below="@+id/linear_sub_layout"
                    android:layout_marginTop="10dp"
                    android:text="@string/choose_file"
                    android:textSize="15sp" />

                <LinearLayout
                    android:id="@+id/linear_sub_layout1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_date"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_choose_file"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:orientation="horizontal"
                    android:weightSum="11" >

                    <ImageView
                        android:id="@+id/iv_choose_image1"
                        android:layout_width="0dp"
                        android:layout_height="100dp"
                        android:layout_weight="3.5"
                        android:background="@drawable/select_image" />

                    <ImageView
                        android:id="@+id/iv_interval_image1"
                        android:layout_width="0dp"
                        android:layout_height="100dp"
                        android:layout_weight=".25" />

                    <ImageView
                        android:id="@+id/iv_choose_image2"
                        android:layout_width="0dp"
                        android:layout_height="100dp"
                        android:layout_weight="3.5"
                        android:background="@drawable/select_image" />

                    <ImageView
                        android:id="@+id/iv_interval_image2"
                        android:layout_width="0dp"
                        android:layout_height="100dp"
                        android:layout_weight=".25" />

                    <ImageView
                        android:id="@+id/iv_choose_image3"
                        android:layout_width="0dp"
                        android:layout_height="100dp"
                        android:layout_weight="3.5"
                        android:background="@drawable/select_image" />
                </LinearLayout>

                <TextView
                    android:id="@+id/txt_lbl_add_bill_and_expense_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/linear_sub_layout1"
                    android:layout_below="@+id/linear_sub_layout1"
                    android:layout_marginTop="10dp"
                    android:text="@string/description"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/edt_add_bill_and_expense_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/txt_lbl_add_bill_and_expense_description"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/txt_lbl_add_bill_and_expense_description"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/edit_text_border"
                    android:ems="10"
                    android:gravity="top"
                    android:inputType="textMultiLine"
                    android:lines="5" />

                <Button
                    android:id="@+id/btn_add_bill_and_expense_add_plus"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/edt_add_bill_and_expense_description"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/blue_button"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/add_plus"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_add_bill_and_expense_add"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_below="@+id/edt_add_bill_and_expense_description"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="10dp"
                    android:layout_toLeftOf="@+id/btn_add_bill_and_expense_add_plus"
                    android:background="@drawable/blue_button"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/add"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_add_bill_and_expense_cancel"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_below="@+id/edt_add_bill_and_expense_description"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="10dp"
                    android:layout_toLeftOf="@+id/btn_add_bill_and_expense_add"
                    android:background="@drawable/blue_button"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp"
                    android:text="@string/cancel"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/iv1"
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_alignParentBottom="true"
                    android:layout_below="@+id/btn_add_bill_and_expense_add"
                    android:textSize="15sp"
                    android:visibility="invisible" />
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

</RelativeLayout>

布局中的此ID android:id="@+id/txt_lbl_add_bill_and_expense_amount_spent"会导致强制关闭

所以我的类文件是引用

mTvAddBillAndExpense = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense);
        mTvAddBillAndExpenseTitle = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_title);
        mTvAddBillAndExpenseFor = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_for);
        mTvAddBillAndExpenseUserName = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_username);
        mTvExpenseDateOfExpense = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_date);
        mTvExpenseType = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_type);
        mTvExpenseVendorName = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_vendor_name);
        mTvExpenseBillNumber = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_bill_number);
        mTvExpenseAmountSpent = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_amount_spent);
        mTvExpenseCurrencyCode = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_currency_code);
        mTvExpenseChooseFile = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_choose_file);
        mTvExpenseDescription = (TextView) findViewById(R.id.txt_lbl_add_bill_and_expense_description);


        mEdtExpenseVendorName = (EditText) findViewById(R.id.edit_add_bill_and_expense_vendor_name);
        mEdtExpenseAmountSpent = (EditText) findViewById(R.id.edit_add_bill_and_expense_amount_spent);
        mEdtExpenseBillNumber = (EditText) findViewById(R.id.edit_add_bill_and_expense_bill_number);
        mEdtExpenseCurrencyCode = (EditText) findViewById(R.id.edit_add_bill_and_expense_currency_code);
        mEdtExpenseDescription = (EditText) findViewById(R.id.edt_add_bill_and_expense_description);

        mChkITEligible = (CheckBox) findViewById(R.id.chk_bill_expense_it_eligible);

        mBtnExpenseDate = (Button) findViewById(R.id.btn_add_bill_and_expense_date);
        mBtnExpenseAddPlus = (Button) findViewById(R.id.btn_add_bill_and_expense_add_plus);
        mBtnExpenseAdd = (Button) findViewById(R.id.btn_add_bill_and_expense_add);
        mBtnExpenseCancel = (Button) findViewById(R.id.btn_add_bill_and_expense_cancel);

        mIvChooseFile1 = (ImageView) findViewById(R.id.iv_choose_image1);
        mIvChooseFile2 = (ImageView) findViewById(R.id.iv_choose_image2);
        mIvChooseFile3 = (ImageView) findViewById(R.id.iv_choose_image3);

和我的错误

12-23 14:14:10.450: E/AndroidRuntime(2065): FATAL EXCEPTION: main
12-23 14:14:10.450: E/AndroidRuntime(2065): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tcl.kushalam/com.tcl.kushalam.AddBillAndExpenseActivity}: java.lang.ClassCastException: android.widget.LinearLayout
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.os.Looper.loop(Looper.java:130)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at java.lang.reflect.Method.invoke(Method.java:507)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at dalvik.system.NativeStart.main(Native Method)
12-23 14:14:10.450: E/AndroidRuntime(2065): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
12-23 14:14:10.450: E/AndroidRuntime(2065):     at com.tcl.kushalam.AddBillAndExpenseActivity.onCreate(AddBillAndExpenseActivity.java:340)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 14:14:10.450: E/AndroidRuntime(2065):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

1 个答案:

答案 0 :(得分:0)

我认为您的类文件中对此ID的引用是 TextView之外的其他内容,或者清理项目。请检查代码中的ID引用。