简体   繁体   中英

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

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.

In styles.xml create a new theme.

    <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.

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

This link will help you.

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

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" />

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