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
setContentView(). This will not start yourSelectPetsScreenActivity and hence You will never have the button click listener assigned to it. Because you are still in theMainActivity:)