How to open an Activity via a button

Asked

Viewed 1,541 times

1

I’m having a problem opening my Activity on various forums and I haven’t found the answer. I have the Activity "Teladelogin" and the Activity "Feed", and I have tried to open in various ways, here is the xml code of the button:

<Button
        android:id="@+id/BtnEntrar"
        style="@style/BotaoComFundo"
        android:layout_marginTop="@dimen/espacamento_Pequeno"
        android:text="@string/entrar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editTextSenha"
        app:layout_constraintVertical_bias="0.0"
        android:onClick="logar"/>

following the ways I tried to program the button by java code:

import android.app.Fragment;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TelaDeLogin extends AppCompatActivity {
    Button button;

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

        button = (Button) findViewById(R.id.BtnEntrar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logar(v);
            }
        });
    }
    public void logar(View v) {
        Intent intent = new Intent(TelaDeLogin.this, Feed.class);
        startActivity(intent);
    }
}

Before it was like this:

import android.app.Fragment;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class TelaDeLogin extends AppCompatActivity {

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

         public void logar(View view){
        Intent i = new Intent(TelaDeLogin.this, Feed.class);
        startActivity(i);
    }

}

In view of the codes, the public class from my feed is stated as follows:

public class Feed extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

and I realized that all the screens that have the implement of Navigation View to the Toolbar and the FeedDrawer I can’t open through a button, so how do I open the Feed through the button, because whenever I click on the button of an error, however on the profile screen that did not have the "implement" of Navigationview opened, and if you put the "implement" Navigation View error and closes the app too.

PS: the Feed page normally opens if you put to open it first, I just can’t access it through the button

1 answer

1


It’s no secret, you can do it inside the onClick straightforward.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), Feed.class);
            startActivity(intent);
        }
    });
  • the app closes in the emulator and gives this error : java.lang.Illegalstateexception: This Activity already has an action bar supplied by the window Color. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your Theme to use a Toolbar Instead.

  • I was able to tidy up, I had put it in the xml action bar but not in the manifest Theme, vlw <3

Browser other questions tagged

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