首页 > 解决方案 > ClassCastException in calling methods added class

问题描述

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.

标签: javaandroidclasscastexception

解决方案


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();

推荐阅读