How I handle this error [closed]

-3

I'm new to programming for android, I know java and c #, I want to push the button and change the text of the label I just managed to make it appear on toast

    package gabriel.meaperte;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.TextView.*;

public class MainActivity extends AppCompatActivity;

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

    public void exibirmensagem(View view){

        Toast.makeText(MainActivity.this, "Você apertou o botão", Toast.LENGTH_LONG).show();

    }

}

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Aperte o botão"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.174"
    app:layout_constraintHorizontal_bias="0.502"
    android:id="@+id/textView" />

<Button
    android:id="@+id/button"
    android:layout_width="159dp"
    android:layout_height="85dp"
    android:text="Botão"
    android:onClick="exibirmensagem"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.55"
    app:layout_constraintHorizontal_bias="0.502" />

    
asked by anonymous 31.08.2017 / 05:31

1 answer

1

You need to find the view by its ID within your layout, after you have set the text you need. Check google documentation

Follow your code as an example:

package gabriel.meaperte;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.TextView.*;

public class MainActivity extends AppCompatActivity;

    private TextView textView;

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

        // Localiza a View através do seu id
        textView = (TextView) findViewById(R.id.textView);
    }

    public void exibirmensagem(View view){
        // Altera o valor da View      
        textView.setText("Seu novo valor no label");
        Toast.makeText(MainActivity.this, "Você apertou o botão", Toast.LENGTH_LONG).show();

    }

}
    
31.08.2017 / 07:13