Error while casting Edittext

Asked

Viewed 420 times

2

I have a problem in my project saving the fields after the changes. On the detail screen I have a change button that when I click it plays to this screen that is below: I believe the error should be in XML, because if I just play an Edittext on the screen I work. Does anyone know why ? The mistake is that he cannot cast in Edit Text.

start activity ComponentInfo{br.com.aula.primeirobanco/br.com.aula.primeirobanco.Editar}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

My xml is like this:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp">

        <ImageView
            android:id="@+id/iconeFilme"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_people_icon"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="pin" />

        <TextView
            android:id="@+id/alteracaoNome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="34dp"
            android:layout_marginStart="34dp"
            android:layout_toRightOf="@+id/iconeFilme"
            android:text="@string/detalhesNome"
            android:textSize="18sp" />


    </LinearLayout>

But in my JAVA code it gives error:

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

    id = (String) getIntent().getExtras().get("id");
    nome = (String) getIntent().getExtras().get("nome");
    sinopse = (String) getIntent().getExtras().get("sinopse");
    duracao = (String) getIntent().getExtras().get("duracao");

    gravarAlteracao = (Button) findViewById(R.id.botaoGravarAlteracao);
    nomeAlterado = (EditText) findViewById(R.id.alteracaoNome);
    sinopseAlterada = (EditText) findViewById(R.id.alteracaoSinopse);
    duracaoAlterada = (EditText) findViewById(R.id.alteracaoDuracao);

I’ll attach the images for better understanding:

XML: XML da alteração

JAVA: Java da alteração

  • 1

    alteraçãoNome is stated in the xml as a Textview and not as a Edittext

  • @ramaral Even if I put the nameAltered = (Textview) findViewById(R.id.alteracaoName); it gives error and prompts to cast and switch to Edittext.

  • 1

    That’s because he’s (I don’t suppose I can see) declared in java as Edittext.

  • 1

    @ramaral That’s right. Many thanks again for the help.

1 answer

2


In your xml:

<TextView
    android:id="@+id/alteracaoNome".....

In java:

 nomeAlterado = (EditText) findViewById(R.id.alteracaoNome);

As stated in the error, it is not possible to perform the cast between different objects!

If the user will edit the field, this must be a EditText, then it will be necessary to change the xml .

If the data will be for viewing only, then it will be necessary to change your Java.

Example:

TextView nomeAlterado;

 nomeAlterado = (TextView) findViewById(R.id.alteracaoNome);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.