0

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.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Eman87
  • 2,735
  • 7
  • 36
  • 53
  • is the main activity inside of a jar? – Martin Cazares Jan 07 '14 at 22:48
  • 1
    I think it's better to use relative class names in the xml. Try changing this line android:name="com.emy.healthytips.MainActivity" to just android:name=".MainActivity" – dberm22 Jan 07 '14 at 23:01
  • @MartinCazares Which jar you mean?? and how I know that it is inside the jar? – Eman87 Jan 07 '14 at 23:44
  • @dberm22 I tried this, but this not fix the issue. – Eman87 Jan 07 '14 at 23:44
  • Do you have any static initialisation code in MainActivity? – NigelK Jan 08 '14 at 00:31
  • @NigelK yes I have too much static variables in MainActivity. – Eman87 Jan 08 '14 at 00:56
  • I don't mean static variables as such, but any static initialisation code for them. An exception in such code will stop the class from loading. A NoClassDefFoundError error is where your class is found at compile time but cannot be created at runtime and this is one possibility. Post your MainActivity code if you think it may be relevant. – NigelK Jan 08 '14 at 01:03
  • Pls post your MainActivity code as the error says "java.lang.NoClassDefFoundError: com.emy.healthytips.MainActivity" – Ramakishna Balla Jan 08 '14 at 04:51
  • @NigelK, No there are no static initialisation code in MainActivity. I added a link to my MainActivity code in my Question. see the edition. Hope that my issue to fix. – Eman87 Jan 08 '14 at 05:19
  • Clean your project and run it again. – Piyush Jan 08 '14 at 13:10

1 Answers1

0

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.

Community
  • 1
  • 1
dberm22
  • 3,187
  • 1
  • 31
  • 46