Start with Splash Screen

Asked

Viewed 94 times

0

I have a project and use the Navigation Drawer Activity, and there was the need to put a Splash screen at the beginning of the project, I tried to put and now is giving an error when opening the project.

I created a Activity to be the Plash screen:

public class TelaSplash extends AppCompatActivity {

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

        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();
            }
        }, 2000);
    }

}

The Layout of the Screen:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TelaSplash">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/background_tela_splash" />

</android.support.constraint.ConstraintLayout>

I also changed Manifest to put Activity as LAUNCHER: The MainActivity is the main of Drawer Activity and the TelaSplash was the servant.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">

        <activity android:name=".MainActivity"/>
        <activity
            android:name=".TelaSplash"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

And here’s Logcat’s bug:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expresso1002/com.example.expresso1002.TelaSplash}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
        at android.app.ActivityThread.access$700(ActivityThread.java:168)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.example.expresso1002.TelaSplash.onCreate(TelaSplash.java:16)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362) 
        at android.app.ActivityThread.access$700(ActivityThread.java:168) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:5493) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:525) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041) 
        at dalvik.system.NativeStart.main(Native Method)

1 answer

0


When using the theme Apptheme.Noactionbar already face your Activity will not have a toolbar (where the title is). So getSupportActionBar() return null, unless, of course, you have called setSupportActionBar(Toolbar) before.

What I have to suggest is not to make Telasplash inherit from AppCompatActivity and use ordinary Activity instead. Some changes apply when this is done, for example, there is no supportActionBar. Since it is a splash, it is interesting to be in "full screen", but preserving the bars of the system (status and navigation).

Androidmanifest

<activity
    android:name=".TelaSplash"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

Telasplash.java

public class TelaSplash extends Activity {

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Pintar a barra de status
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();
            }
        }, 2000);
    }

}
  • Perfect, it worked 100%.

  • I didn’t understand the part about using ordinary Activity, which would be ?

  • android.support.v7.app.AppCompatActivity - Base class for activities that use the resources of the support library action bar vs "ordinary" (normal) android.app.Activity

  • Dough, vlw even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.