Search code examples
javaandroidsplash-screencountdowntimeroncreate

Creating Splash Screen using CountDownTimer class


It gives an error on OnCreate as :

at com.explore.sargam.activity.LaunchScreenActivity.onCreate(LaunchScreenActivity.java:17)

The below is the code to The launcher activity which inflates the the splash screen

public class LaunchScreenActivity extends AppCompatActivity {


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

        new CountDownTimer(3000,0){
           @Override
            public void onTick(long millisUntilFinished)
            {

            }
            @Override
            public void onFinish(){
                Intent i = new Intent(LaunchScreenActivity.this,MainActivity.class);
                startActivity(i);
                finish();
            }
        }.start();



    }


    @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_launch_screen, 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);
    }


}

And the below is the splash screen layout file

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash"
        />

</FrameLayout>

</RelativeLayout>

This is the cat log

03-15 15:06:10.364    2875-2875/com.explore.sargam E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.explore.sargam, PID: 2875
    java.lang.OutOfMemoryError: Failed to allocate a 426688012 byte allocation with 1036624 free bytes and 95MB until OOM
            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
            at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
            at android.content.res.Resources.loadDrawable(Resources.java:2540)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
            at android.view.View.<init>(View.java:3948)
            at android.widget.ImageView.<init>(ImageView.java:145)
            at android.widget.ImageView.<init>(ImageView.java:140)
            at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:58)
            at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:54)
            at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
            at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.explore.sargam.activity.LaunchScreenActivity.onCreate(LaunchScreenActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Solution

  • This error is due to large size of the image you are using . You need to scale the image down I recommend you to use the picasso library to scale down the image. the link for Picasso or just use a small size image in the splash screen