Error casting EditText

2

I have a problem in my project saving the fields after the changes. On the details 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 the XML, because if I just play an EditText on the screen I work. Does anyone know why? The error is that it can not 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 looks 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 will attach the images for better understanding:

XML:

JAVA:

    
asked by anonymous 01.11.2016 / 11:31

1 answer

2

In your xml:

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

No java:

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

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

If the user will edit the field, it should be EditText , so it will be necessary to change the xml .

If your data is just for viewing, you will need to change your Java .

Example:

TextView nomeAlterado;

 nomeAlterado = (TextView) findViewById(R.id.alteracaoNome);
    
01.11.2016 / 21:24