简体   繁体   中英

Open New Activity from Android Button Click

In my app I'm developing in android studio, I'm making a button to redirect from one activity to another but it does not give me anything even though I do not have any errors in the code.

package com.example.shreeganesha.splash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_beatness);
    init();
}
    public Button btn2;

 public void init(){
         btn2= (Button)findViewById(R.id.btn2);
         btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent inf= new Intent(beatness.this,Informacoes.class);

              startActivity(inf);
            }
      });
}

Try to declared it on your manifest file your "Informacoes" class as an activity.

Try to organize your code better as well. It's very messy.

Check your id's on your xml layout, also check there is no onClick XML property being used that can be conflicting with your button, check that your new activity name and that your new activity is correctly linked to the intended layout

package com.example.shreeganesha.splash;

    private Button changeActivity; //Try more explicit names on your variables instead of just btn2, 
    // also declare your variables on top for better organized code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_beatness);
        init();
    }


    public void init(){
        changeActivity = (Button)findViewById(R.id.changeActivity);
        changeActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(this, YourNewActivityClass.class);
                startActivity(i);
            }
        });
    }

Hope it helps

Check your id's on your xml layout, also check there is no onClick XML property being used that can be conflicting with your button, check that your new activity name and that your new activity is correctly linked to the intended layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM