NoClassDefFoundError in SplashScreen

535 Views Asked by At

In my App, it opens a Splash Screen then MainActivity. I wrote the following code

SplashActivity.java

    public class SplashActivity extends Activity {

    private final int SPLASH_DISPLAY_LENGHT = 2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}

MainActiviy.java

here

And I added both MainActivity and SplashActivity to manifest as following:

    <activity
        android:name="com.emy.healthytips.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>

    <activity
        android:name="com.emy.healthytips.MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:launchMode="singleTop">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".MainActivity" />

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

         <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="com.emy.healthytips.MainActivity"
                android:scheme="oauth" />
        </intent-filter>
    </activity>

But it gives me the following Exception

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.emy.healthytips.MainActivity
at com.emy.healthytips.SplashActivity$1.run(SplashActivity.java:20)

In this line

Intent intent = new Intent(SplashActivity.this, MainActivity.class);

How can I fix this? Hope anyone can help me. Thanks in advance.

1

There are 1 best solutions below

0
On

Multiple things...

1) The way you are doing it defeats the purpose of a splash screen. Splash screens are supposed to give the user a pretty picture while the app loads in the background. All you are doing is adding an extra 2s delay. Take a look at this post: Android SplashScreen

2) Many people say that this method does not work on <4.0. Not sure why, but just a heads up (https://stackoverflow.com/a/5486970/2066079)

3) instead of:

startActivity(intent);
finish();

you should use:

SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();

You want to use the activity's version of startActivity() instead of the Runnable's. This might be unneccessary, but if it doesn't help, atleast it's good practice.

4) Also, like I mentioned in my comment, using android:name=".MainActivity" instead of android:name="com.emy.healthytips.MainActivity" in the xml is preferred to eliminate possible unchecked typo errors.