Android studio Activity does not open with click button

Asked

Viewed 942 times

0

My problem is the following. my application has a welcome screen where the user ckick the "continue" button and goes to the next screen. The next one contains a menu with several buttons. my problem is that I cannot open another Activity on the second screen (on the first screen that opens normal) more or less this scheme below (| activity1> continue button | >> | activity2> Continue2 button |> does not answer |tela3>> does not open) to praise and test the apk on a Galaxy grand duos 4.2.2

code below Code 1 screen (welcome).

public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button7 = (Button) findViewById(R.id.button7);

button7.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.activity_main4);
    }
});}}

code screen 2

public class Main4Activity extends AppCompatActivity {
private  Button prova;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);

prova = (Button) findViewById(R.id.button5);

    prova.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent( Main4Activity.this, 
    Main3Activity.class);
        startActivity(intent);
    }
});

}}

screen 2 input xml code

            <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/button4"
            android:layout_marginTop="11dp"
            android:text="tela 2"/>

anexo imagem

1 answer

0


Hello,

Your App’s problem is in the method onClick of the first Activity, when you execute the method setContentView(R.layout.activity_main4); you are just loading the screen layout, and not passing the processing control to the next activity, then the method onClick of prova.setOnClickListener is not executed. To correct the error, create a Intent to call the Main4Activity. Replace the excerpt from:

setContentView(R.layout.activity_main4);

for:

Intent intentProva = new Intent( Main2Activity.this, 
Main4Activity.class);
    startActivity(intentProva);
  • 1

    very good, worked perfectly. thank you!!!

Browser other questions tagged

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