1

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:

    <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! 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) :

    <?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
  • 2
    hit build -> clean project and when it is done run it again Commented Feb 24, 2016 at 15:34
  • I have tried cleaning the project already but still get the same result Commented Feb 24, 2016 at 15:36
  • @kmil What is the content of line 23 in MainActivity.java? Commented Feb 24, 2016 at 15:40

3 Answers 3

1

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. It looks like you're including this layout in activity_main - would you mind pasting the XML for that, and any other layouts included?

My suspicion is that you have a conflict between two id's.

Sign up to request clarification or add additional context in comments.

Comments

1

you can try this

1 clean code and try again

2 close IDE and rebuild the code again

hope it helps

Comments

1

Check your IDs. Are you using etEmail and etPassword somewhere else in the project?

Are those ids uniquely associated to an EditText?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.