Android Switch between activities

android, android-activity, screen, switch-statement

Solution

Here you aren't ever switching to the next `Activity`, just changing the `layout` of the current `Activity`

 new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setContentView(R.layout.selectscreen); //where <next> is you target      activity :)
                }
            }, 5000);  

instead of `setContentView()` you need to use an `Intent`

Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);

Since you aren't actually going to the next `Activity` (java file) your `onClick()` isn't set.

Edit

This is what you are doing

public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Delay Code after 5 seconds
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            setContentView(R.layout.selectscreen); //where <next> is you target      activity :)
            }
        }, 5000);   
}

This is what you should be doing. Notice the difference in the `run()` function

public class MainActivity extends Activity {
Button fButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Delay Code after 5 seconds
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
            startActivity(i);
            }
        }, 5000);   
}

Problem

what i'm trying to do is a home screen that stays for 5 seconds and goes to activity1.When i click a button in activity1 leads me to activity2.I've tried many times to click the button but no switching happens. homescreen (5 seconds)=Main_Activity Activity1=selectpets.java Activity2=fishtank.java onclick listener seems the problem i don't know what's wrong with it ``` Main Activity Code package com.set.petshome; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button fButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); } //Delay End @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ``` Now the Selectpets Code ``` package com.set.petshome; import android.app.Activity; import android.content.*; import android.os.Bundle; import android.view.*; import android.widget.Button; public class SelectPetsScreen extends Activity { Button fButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectscreen); //Button Fishtank Listener Start fButton = (Button) findViewById(R.id.button1); //Listening to button event fButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { //Starting a new Intent Intent nextScreen = new Intent(getApplicationContext(), fishtank.class); startActivity(nextScreen); } }); //Button Fishtank Listener End } } ``` Fishtank class code ``` package com.set.petshome; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; public class fishtank extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ftank); } } ``` by the way no errors in the application just no switching after clicking thank you very much

Original source