java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText

时间:2016-02-24 15:32:37

标签: java android android-layout

在发布之前,我已经阅读了许多与我相似的问题,但没有运气。

02-24 15:12:11.334 22856-22856/k.testproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: k.testproject, PID: 22856
                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{k.testproject/k.testproject.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                            Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
                                                               at k.testproject.MainActivity.onCreate(MainActivity.java:23)
                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:148) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

当我确定错误出现时,代码中的部分是:

public class MainActivity extends AppCompatActivity {

EditText etEmail, etPassword;
Button signInButton;
UserLocalStore userLocalStore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etEmail = (EditText) findViewById(R.id.etEmail);
    etPassword = (EditText) findViewById(R.id.etPassword);
    signInButton = (Button) findViewById(R.id.signInButton);

与该部分相关的特定XML文件是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="20dp"
    android:layout_marginTop="70dp"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-Mail" />

    <EditText
        android:id="@+id/etEmail"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
    <Button
        android:id="@+id/signInButton"
        android:text="Sign In"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

此异常的原因是什么?我是如何修复的?

当我使用线性时,我不完全确定为什么异常与相对布局有关。

请注意我对Android很新,所以这可能不是最明智的问题!感谢您的帮助!

请求

activity_main.xml代码(据我所知,我自己没有创建或触摸过这个文件):

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="k.testproject.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include
        android:id="@+id/etEmail"
        layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:1)

你可以试试这个

1个干净的代码,然后重试

2关闭IDE并重新生成代码

希望有所帮助

答案 1 :(得分:1)

清理/重建您的项目以查看是否修复了该项目。如果不是:

您的XML看起来很好,代码的小片段也是如此 - 所以这很难回答。看起来您在activity_main中包含了这个布局 - 您是否介意为此粘贴XML以及包含的任何其他布局?

我怀疑你们两个身份之间存在冲突。

答案 2 :(得分:1)

检查您的ID。你在项目的其他地方使用etEmailetPassword吗?

这些ID是否与EditText唯一关联?

相关问题