classcastexception edittext无法强制转换为android.view.viewgroup

时间:2012-08-21 11:56:47

标签: android xml

以下是我的xml文件的代码

  <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editview_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:filterTouchesWhenObscured="true"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/userid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="USER ID"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/edituserid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/editbox_background"
            android:scrollHorizontally="true"
            android:textColor="#333399" />

        <TextView
            android:id="@+id/usertype"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="USER TYPE"
            android:textSize="8pt" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:entries="@array/spinner" />

        <TextView
            android:id="@+id/company"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="COMPANY"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/editcompany"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:textColor="#333399" />

        <TextView
            android:id="@+id/firstname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FIRST NAME"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/editfirstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:textColor="#333399" />

        <TextView
            android:id="@+id/lastname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LAST  NAME"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/editlastname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:textColor="#333399"
             />

        <TextView
            android:id="@+id/encryptionkey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ENCRYPTION KEY"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/editencryption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background" >

            <TextView
                android:id="@+id/lasttagupdate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LAST TAG UPDATE"
                android:textSize="8pt" />

            <DatePicker
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="10dip"
                android:layout_weight="1"
                android:endYear="2100"
                android:startYear="1900" />

            <Button
                android:id="@+id/ok"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:text="SAVE" />
        </EditText>
    </LinearLayout>

</ScrollView>

和我使用此布局的文件

public class writeRFIDTag extends Activity {
    EditText userId, company, firstName, lastName, encryptionKey;
    Button save;
    Spinner spin;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rfid_write);
        userId = (EditText) findViewById(R.id.edituserid);
        company = (EditText) findViewById(R.id.editcompany);
        firstName = (EditText) findViewById(R.id.editfirstname);
        lastName = (EditText) findViewById(R.id.editlastname);

    }

}

和我的logcat

08-21 17:14:51.410: E/AndroidRuntime(20353): FATAL EXCEPTION: main
08-21 17:14:51.410: E/AndroidRuntime(20353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.securityapp/com.example.securityapp.writeRFIDTag}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.view.ViewGroup
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.os.Looper.loop(Looper.java:137)
08-21 17:14:51.410: E/AndroidRuntime(20353):    at android.app.ActivityThread.main(ActivityThread.java:4424)

2 个答案:

答案 0 :(得分:0)

请注意,您忘记在EditText之后结束。

[1] 正如我所示,使用/签署结束它 [2] 最后从XML中删除。

    ......
    ......
    ......
        <TextView
            android:id="@+id/encryptionkey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ENCRYPTION KEY"
            android:textSize="8pt" />

        <EditText
            android:id="@+id/editencryption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background" />
                                                                      ^  Add this ENDING sign here
                                                                      ^

            <TextView
                android:id="@+id/lasttagupdate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LAST TAG UPDATE"
                android:textSize="8pt" />

            <DatePicker
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="10dip"
                android:layout_weight="1"
                android:endYear="2100"
                android:startYear="1900" />

            <Button
                android:id="@+id/ok"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:text="SAVE" />

     //     </EditText>    <--  REMOVE THIS FROM YOUR XML

    </LinearLayout>

</ScrollView>

答案 1 :(得分:0)

EditText不是ViewGroup。您可以使用不同的布局(如RelativeLayout)重叠放置其他组件。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<EditText
        android:id="@+id/editencryption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background" />

<TextView
            android:id="@+id/lasttagupdate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="LAST TAG UPDATE"
            android:textSize="8pt" />

        <DatePicker
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="10dip"
            android:layout_weight="1"
            android:endYear="2100"
            android:startYear="1900" />

        <Button
            android:id="@+id/ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="SAVE" />

</RelativeLayout>