6

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

2
  • You are changing the setContentView(). This will not start your SelectPetsScreen Activity and hence You will never have the button click listener assigned to it. Because you are still in the MainActivity :) Commented Aug 15, 2013 at 12:47
  • Did you add second Activity to Manifest.xml? If yes, try to add to SelectPetsScreen: ` android:launchMode="singleTask"` Commented Aug 15, 2013 at 13:07

4 Answers 4

4

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); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes i know i want to switch from activity1 to activity2 and homescreen changes to activity1 which you're talking about.the code you mentioned i know that's not changing the activity.take a look again you'll see that your code i already used it but not change activity1 to 2 :D
No, in your MainActivity you are not doing this. Look at my answer again. You are not using an Intent there to start the next Activity which is where your onClickListener is set. You are JUST changing the layout and not going to the Activity.
Now i get it but after changing the code System UI stops(crashes) on MainActivity
I can't tell you why without knowing what the error is. What's it say in the logcat for Fatal Exception and what is the first line that references your package after that?
1

You can use finish() if you want use firts Activity only once.

mHandler.postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent().setClass(MainActivity.this, SelectPetsScreen .class).setData(getIntent().getData())); finish(); } }, 5000); 

Be sure that you have 2nd Activity defined in your Manifest.xml:

<activity android:name="x.x.SelectPetsScreen" android:theme="@style/NoTitle" android:screenOrientation="nosensor" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity> 

3 Comments

@user1928775 Did you add second Activity to Manifest.xml?
@user1928775 try to add to SelectPetsScreen: in manifest.xml: ` android:launchMode="singleTask"`
Thanks Maxim Shoustin and everyone it worked after adding SelectPetsScreen in manifest.xml
1

Avoid doing this.

 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); 

Google gives you the Intent mechanism for switching Activities

i.e. use

startActivity(new Intent(this, yourSecondActivity.class)); 

instead of

setContentView(R.layout.selectscreen); 

The remaining part of your code must work fine.

Comments

0

I was able to solve it with the help of Maxim Shoustin and everyone by: adding second Activity to Manifest.xml which is SelectPetsScreen Thank you so much

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.