open activity from another activity after some seconds


open activity from another activity after some seconds



I try to open a new activity from another one after some seconds, I used this code,
but it's not working (first activity runs but after some seconds I have an error)


public class WelcomeActivity extends AppCompatActivity {

private static int TIME_OUT = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
//finish();
}
}, TIME_OUT);
}
}



This is a stacktrace:



java.lang.RuntimeException: Unable to start activity
ComponentInfo{________}: java.lang.IllegalStateException: This
Activity already has an action bar supplied by the window decor. Do
not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar
to false in your theme to use a Toolbar instead





What error are you getting?
– user8035311
Jun 30 at 18:31





java.lang.RuntimeException: Unable to start activity ComponentInfo{________}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
– Yaman Melhem
Jun 30 at 18:39




2 Answers
2



The problem is not with this code. According to your exception the problem is in the MainActivity class. I think your are trying to create custom action bar. But the error is occurring due to your current theme already have an action bar. So you need customize your theme for that class.


MainActivity class



In styles.xml create a new theme.


styles.xml


<style name="Theme.FullScreen" parent="AppTheme.NoActionBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowActionBar">false</item>
</style>



In the AndroidManifest.xml change the theme of your view to your customized theme.


AndroidManifest.xml


<activity
android:name=".MainActivity "
android:theme="@style/Theme.FullScreen" />



This link will help you.



make
<item name="windowActionBar">false</item> in your style.xml


<item name="windowActionBar">false</item>



Example:


<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/AnimationActivity</item>

</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />





thank you very much, your answer is correct also
– Yaman Melhem
Jun 30 at 19:17






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API