-1

Sorry if this one is a stupid question.

I am trying to do this Android global variable

I added a new class but I cannot call the methods inside the class. I get:

inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.appname.classname'

here is the code inside the new class:

public class GlobalVars extends Application {
    private static int lvl;
    public int getLvl() {
        return lvl;
    }
    public void setLvl(int lvl) { 
        lvl = this.lvl;
    }
}

my manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.android.guessinggame">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        android:screenOrientation="sensorLandscape"
        <activity android:name=".MainActivity">
            android:screenOrientation="sensorLandscape"
            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=".enterword"></activity>
        <activity android:name=".player1turn"></activity>
        <activity android:name=".aiturn"></activity>
        <activity android:name=".chooselevel"></activity>
        <application android:name=".GlobalVars"/>
    </application>

</manifest>

then in the activity:

protected void select1 (View view) {
    ((GlobalVars) this.getApplication()).setLvl(1); <-------- error
    Intent intent = new Intent(this, enterword.class);
    startActivity(intent);
}

this produces:

inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.guessinggame.GlobalVars'

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.app.android.guessinggame.GlobalVars

Solved

in the manifest, the name must be defined. not add. changed it to:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.android.guessinggame">

<application
    android:name=".GlobalVars"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    android:screenOrientation="sensorLandscape"
    <activity android:name=".MainActivity">
        android:screenOrientation="sensorLandscape"
        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=".enterword"></activity>
    <activity android:name=".player1turn"></activity>
    <activity android:name=".aiturn"></activity>
    <activity android:name=".chooselevel"></activity>
</application>

</manifest>

now it works.

Zoe
  • 27,060
  • 21
  • 118
  • 148
rapture
  • 1
  • 3

1 Answers1

1

If you want to make GlobalVars your Application class in Android you have to make it extend android.app.Application.

You also have to declare this in the Manifest.

On a side note, there is only one instance of Application. You may not need to make lvl static.

public class GlobalVars extends Application{
    private static int lvl;
    public int getLvl() {
        return lvl;
    }
    public void setLvl(int lvl) { 
        lvl = this.lvl;
    }
}

In the manifest you have to have:

<application
        android:name=".GlobalVars"
        ...
</application>

In your activity, in onCreate() for example:

((GlobalVars) getApplication()).getLvl();
Juan
  • 5,525
  • 2
  • 15
  • 26
  • Yes sir. in the androidmanifest.xml i added: – rapture May 17 '18 at 02:00
  • For clarity I added the full name of the Application class you have to extend, and I have added how the class should be added to the manifest. I have just tried it locally with a project of mine and it is working. – Juan May 17 '18 at 02:17
  • By the way after changing the manifest yo need to synchronize the project in Android Studio. – Juan May 17 '18 at 02:20
  • I have seen your manifest, you have nested another level of Application. To the original manifest you only need to add the name property to the existing application tag. Put it on top of allowBackup=true – Juan May 17 '18 at 02:23
  • hhmm...so I need to override the name too. not add.. done that and now it works. thanks – rapture May 17 '18 at 02:24
  • The name property of the Application tag tells the app which will be the Application singleton class. – Juan May 17 '18 at 02:26