-1

After the splash screen following error displayed. How to solve this?

This error message is displayed after completing the specified time in thread in SplashScr.java

enter image description here

AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lalinda.splashscreen">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashScr"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Splash"/>
    </application>

</manifest>

SplashScr.java

package com.example.lalinda.splashscreen;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by Lalinda on 8/7/2016.
 */
public class SplashScr extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        Thread myThread = new Thread()
        {
            @Override
            public void run()
            {
                try {
                    sleep(10000);
                    Intent startMainScreen = new Intent(getApplicationContext(), Splash.class);
                    startActivity(startMainScreen);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        myThread.start();
    }
}

Splash.java : This is the second activity

package com.example.lalinda.splashscreen;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class Splash extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_splash, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Edit : logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lalinda.splashscreen/com.example.lalinda.splashscreen.Splash}: 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.

ONE_FE
  • 968
  • 3
  • 19
  • 39

1 Answers1

1

You need to remove the ActionBar, before setting a toolbar. Set your app theme something like:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style> 

Like @Habel Philip mentioned in his comment above, use a Handler to show the Splash screen for a limited time. In fact, I would suggest against using a Splash screen in the first place. This is the recommended pattern now (Launch Screen Pattern): https://www.bignerdranch.com/blog/splash-screens-the-right-way/

Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • *You need to remove the ActionBar, before setting a toolbar* ... what a bs ... 1. he is using AppCompat so deriving style from `@android:style/Theme.Holo.Light` oviously will not help ... 2. he is using `setSupportActionBar` then obviously deriving theme from `@android:style/Theme.Holo.Light` will cause another problem ... **obviously using `setSupportActionBar` with `NoActionBar` theme is mutually exclusive** – Selvin Aug 09 '16 at 14:55
  • @Selvin Chill...1. I didn't give him a supplement code...thats why my text reads Set your app theme **something like**, I was giving him an indication.. 2. Even the error message reads "...set windowActionBar to false in your theme to use a Toolbar instead."...3 I don't understand whats your problem with setting `android:windowActionBar` false and calling `setSupportActionBar()` simultaneously. – Shaishav Aug 09 '16 at 15:07
  • And i do not understand why to touch `android:windowActionBar` while error is talking about `windowActionBar` (yes, it is a difference - the one without namespace is comming from appcompat library) which makes your "answer" useless – Selvin Aug 09 '16 at 15:09