简体   繁体   English

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

[英]java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText

Before posting I have already read many similar questions to mine but have had no luck. 在发布之前,我已经阅读了许多与我相似的问题,但没有运气。

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) 

The portion of the code when I'm sure that the error is emerging is: 当我确定错误出现时,代码的一部分是:

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

And the particular XML file related to the portion is: 与该部分相关的特定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>

What is the reason for this exception and how am I able to fix? 这个例外是什么原因以及我如何解决?

I'm not entirely sure why the exception is relating to relative layout when I am using linear. 当我使用线性时,我不完全确定为什么异常与相对布局有关。

Please note that I am quite new to Android so this may not be the smartest of questions! 请注意我对Android很新,所以这可能不是最明智的问题! Thank you for your help in advance! 提前谢谢你的帮助!

activity_main.xml code as requested (As far as I am aware I have not created nor touched this file myself) : 请求的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>

you can try this 你可以试试这个

1 clean code and try again 1清洁代码,然后再试一次

2 close IDE and rebuild the code again 2关闭IDE并重新生成代码

hope it helps 希望能帮助到你

clean/rebuild your project to see if that fixes it. 清理/重建您的项目,看看是否修复了它。 If not: 如果不:

Your XML looks fine, as does the small snippet of code - so this is hard to answer. 你的XML看起来很好,就像一小段代码一样 - 所以这很难回答。 It looks like you're including this layout in activity_main - would you mind pasting the XML for that, and any other layouts included? 看起来你在activity_main中包含了这个布局 - 你是否介意为此粘贴XML,以及包含的任何其他布局?

My suspicion is that you have a conflict between two id's. 我怀疑你们两个身份之间存在冲突。

Check your IDs. 检查您的ID。 Are you using etEmail and etPassword somewhere else in the project? 你在项目的其他地方使用etEmailetPassword吗?

Are those ids uniquely associated to an EditText? 这些ID是否与EditText唯一关联?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText android.widget.RelativeLayout无法转换为android.widget.EditText - android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 原因:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为com.slidingmenu.lib.SlidingMenu - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout - java.lang.ClassCastException: android.widget.RelativeLayout java.lang.ClassCastException: android.widget.EditText - java.lang.ClassCastException: android.widget.EditText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM