Java Question ClassCastException

Johan Schoeman

Expert
Licensed User
I am trying to do a shortcut wrap for a B4A member. By "shortcut" I mean that the B4A wrapper purely kick starts the original project as a separate activity. I have done it numerous times with similar projects but this project refuses to cooperate. The error that I get is the following:

B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{JHS.SkyTest/com.skytree.epubtest.HomeActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.skytree.epubtest.SkyApplication

The B4A wrapper is simple:
B4X:
public class skytestWrapper extends AbsObjectWrapper<HomeActivity> {

   
    @Hide
    public static BA ba;
    @Hide
    public static String eventName;
    private static HomeActivity cv;
    private IOnActivityResult ion;
   
   
    Intent izx;
   
    public void Initialize(BA paramBA, String paramString) {
        eventName = paramString.toLowerCase(BA.cul);
        ba = paramBA;

        HomeActivity cv = new HomeActivity();
        String str = paramString.toLowerCase(BA.cul);
   
        setObject(cv);
        izx = new Intent(BA.applicationContext, HomeActivity.class);   
    }   


     public void startSkyTest() {
        ion = new IOnActivityResult() {
           
            @Override
            public void ResultArrived(int arg0, Intent data) {
               
            }
        };
        ba.startActivityForResult(ion, izx);
    }
   
   
}

The error occurs in class HomeActivity.java as
B4X:
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.skytree.epubtest.SkyApplication
at com.skytree.epubtest.HomeActivity.onCreate(HomeActivity.java:303)

Line 303 of HomeActivity.java is as follows:
B4X:
app = (SkyApplication)getApplication();
app is declared as type SkyApplication in class HomeActivity.java

The projects Manifest file looks as follows:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skytree.epubtest"
    android:versionCode="2"
    android:versionName="1.2" >

    <!-- tagetgetSdkVersion should be 16. -->
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>   
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
   
   
<application
        android:name="com.skytree.epubtest.SkyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:hapticFeedbackEnabled="false"
        android:hardwareAccelerated="true"
         >
        <activity
            android:name="com.skytree.epubtest.HomeActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"           
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.skytree.epubtest.BookViewActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="BookView"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
        </activity>
        <activity
            android:name="com.skytree.epubtest.MagazineActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="Magazine"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

        <service
            android:name="com.skytree.epubtest.LocalService"
            android:enabled="true" >
        </service>

        <activity
            android:name="com.skytree.epubtest.SettingActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="@string/title_activity_setting" >
        </activity>
    </application>

</manifest>

My B4A manifest file looks as follows:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
'AddApplicationText(android:name = "com.skytree.epubtest.SkyApplication")

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
       
AddApplicationText(<service
            android:name="com.skytree.epubtest.LocalService"
            android:enabled="true" >
        </service>)       
       
AddApplicationText(

<activity
            android:name="com.skytree.epubtest.HomeActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"           
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name="com.skytree.epubtest.BookViewActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="BookView"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
        </activity>
        <activity
            android:name="com.skytree.epubtest.MagazineActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="Magazine"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>


        <activity
            android:name="com.skytree.epubtest.SettingActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="@string/title_activity_setting" >
        </activity>)

I have added the permission that are in the project's Manifest in the wrapper
B4X:
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE", "android.permission.READ_PHONE_STATE",
                     "android.permission.VIBRATE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.ACCESS_WIFI_STATE",
                     "android.permission.WRITE_SETTINGS", "android.permission.CHANGE_WIFI_STATE"})

My question is:
1. Is my Manifest correct and if not is that why I get the error?
2. If not my Manifest - why the ClassCastException?

Will appreciate some help with this.

Thanks

JS
 

Johan Schoeman

Expert
Licensed User
The generated manifest doesn't match the manifest code you posted.

You should set the application class with:
B4X:
SetApplicationAttribute(android:name, "com.skytree.epubtest.SkyApplication")
Thanks Erel - manifest is now looking like this and it is working:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
'AddApplicationText(android:name = "com.skytree.epubtest.SkyApplication")

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "@string/app_name")
SetApplicationAttribute(android:name, "com.skytree.epubtest.SkyApplication")
SetApplicationAttribute(android:largeHeap,"true")
SetApplicationAttribute(android:hapticFeedbackEnabled, "false")
SetApplicationAttribute(android:hardwareAccelerated, "true")
'End of default text.
       
AddApplicationText(<service
            android:name="com.skytree.epubtest.LocalService"
            android:enabled="true" >
        </service>)       
       
AddApplicationText(<activity
            android:name="com.skytree.epubtest.HomeActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"           
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name="com.skytree.epubtest.BookViewActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="BookView"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
        </activity>
        <activity
            android:name="com.skytree.epubtest.MagazineActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="Magazine"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>


        <activity
            android:name="com.skytree.epubtest.SettingActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"
            android:label="@string/title_activity_setting" >
        </activity>)

1.png
 
Top