Sumit Yadav
Sumit Yadav

Reputation: 97

Getting error in my android programming code java.lang

this is my java file UserAreaActivity file. check this code and give ans in code format. m using latest version of android studio.

package com.symplycode.newloginregister;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class UserAreaActivity extends AppCompatActivity {

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

        final EditText etUsername=(EditText) findViewById(R.id.etUsername);
        final EditText etAge=(EditText) findViewById(R.id.etAge);
        final TextView welcomeMessage=(TextView) findViewById(R.id.tvWelcomeMsg);


        Bundle extras=getIntent().getExtras();
        String name= extras.getString("name");
        String username= extras.getString("username");
        String age= extras.getString("age");

        String message = name + "welcome to your user area";
        welcomeMessage.setText(message);
        etUsername.setText(username);
        etAge.setText(age);
    }
}

this is xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="20dp"
    tools:context="com.symplycode.newloginregister.UserAreaActivity">

    <TextView
        android:id="@+id/tvWelcomeMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="24sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvWelcomeMsg"
        android:layout_marginTop="30dp"
        android:paddingLeft="6dp"
        android:text="Name" />

    <TextView
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView10"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/etName"
        android:paddingLeft="6dp"
        android:text="Age" />

    <TextView
        android:id="@+id/etAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView11"
        android:ems="10"
        android:inputType="textPersonName" />
</RelativeLayout>

getting error in this.. how can i solve this please tell me

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.symplycode.newloginregister/com.symplycode.newloginregister.UserAreaActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

Upvotes: 1

Views: 247

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

 final EditText etUsername=(EditText) findViewById(R.id.etUsername);
        final EditText etAge=(EditText) findViewById(R.id.etAge);

must be

 final TextView etUsername=(TextView) findViewById(R.id.etName);
        final TextView etAge=(TextView) findViewById(R.id.etAge);

You are trying to cast an TextView widget to a EditText widget. Either change it like my example or change the TextView inside your xml layout to EditText

By the way, there is no view with the id etUsernameinside your layout, maybe it´s a typo and you mean etName ? Be sure you are using the correct layout xml...

The layout that you set with setContentView(R.layout.activity_user_area); must be the layout where both views are inside...

Upvotes: 1

Related Questions