Search code examples
androidfatal-errorclasscastexceptionruntimeexception

Error while running the App for the first time can not cast issue


E/AndroidRuntime: FATAL EXCEPTION: main Process: de.mywoofi.app, PID: 17970 java.lang.RuntimeException: Unable to instantiate application de.mywoofi.app.MyWoofiApplication_: java.lang.ClassCastException: de.mywoofi.app.MyWoofiApplication_ cannot be cast to android.app.Application at android.app.LoadedApk.makeApplication(LoadedApk.java:1069) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5842) at android.app.ActivityThread.access$1100(ActivityThread.java:199) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.ClassCastException: de.mywoofi.app.MyWoofiApplication_ cannot be cast to android.app.Application at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50) at android.support.v4.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:49) at android.app.Instrumentation.newApplication(Instrumentation.java:1120) at android.app.LoadedApk.makeApplication(LoadedApk.java:1061) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5842) at android.app.ActivityThread.access$1100(ActivityThread.java:199) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The Problem is, that I have no idea, what the exact solution is. I've tried a lot of "solutions" from this page, but maybe I'm to stupid to resolve that problem. If there are any request for some Code, please ask.

Thanks for your help!

The requested Class (shortened)

@EActivity(de.mywoofi.app.R.layout.activity_main)
public class MyWoofiApplication extends Activity
{ [...]
}

And the cast:

<application
    android:name=".MyWoofiApplication_"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:replace="android:icon"
    android:fullBackupContent="true"
    tools:ignore="AllowBackup,GoogleAppIndexingWarning">
    <activity
        android:name=".ui.activity.SplashActivity_"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Full"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

My updated Manifest

<application
    android:name=".ApplicationClass"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:replace="android:icon"
    android:fullBackupContent="true"
    tools:ignore="AllowBackup,GoogleAppIndexingWarning">
    <activity
        android:name=".ui.activity.SplashActivity_"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Full"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

here my ApplicationClass.java

public class ApplicationClass extends Application{

@Override
public void onCreate(){
    super.onCreate();
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectAll()
    .penaltyLog()
    .build());
}

}


Solution

  • Application need to extends Application and mentioned in Manifest file & MainActivity extends Activity

    You got error because your MyWoofiApplication_ is not extend Application it's extend Activity so got that's why you got this error:

    de.mywoofi.app.MyWoofiApplication_ cannot be cast to android.app.Application
    

    Application Class

    public class ApplicationClass extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build());
        }
    }
    

    Manifest File

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="YOUR_PACKAGE_NAME">
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:name=".ApplicationClass"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".SplashActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait"
                android:theme="@style/AppTheme.NoActionBar"
                android:windowSoftInputMode="adjustResize" />
        </application>
    

    MainActivity.java

    public class MainActivity extends Activity {    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            setContentView(R.layout.activity_main);       
        }
     }
    

    Hope you understand now and it may help to you.