Android全局变量实现遇ClassCastException问题及解决
嗨,这问题一点都不低级,很多刚接触Android全局变量的开发者都会踩这个坑,我来帮你梳理清楚问题和解决办法~
问题描述
你尝试通过继承Application类实现Android全局变量,但在Activity中调用自定义类方法时出现类型转换错误:inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.guessinggame.GlobalVars'
你的自定义Application类代码
public class GlobalVars extends Application { private static int lvl; public int getLvl() { return lvl; } public void setLvl(int lvl) { lvl = this.lvl; } }
原错误的AndroidManifest配置
<?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>
Activity调用代码(报错行标注)
protected void select1 (View view) { ((GlobalVars) this.getApplication()).setLvl(1); // <-------- 报错行 Intent intent = new Intent(this, enterword.class); startActivity(intent); }
完整报错信息
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
问题原因
你在Manifest里错误地新增了一个嵌套的子<application>标签来指定自定义Application类,这不符合AndroidManifest的语法规范。系统只会识别根节点的<application>标签,完全不会读取嵌套的子标签,所以系统依然使用默认的android.app.Application类,自然就会出现类型转换失败的问题。
解决方法
直接在根<application>标签中添加android:name=".GlobalVars"属性即可,不需要新增子标签。修改后的Manifest配置如下:
<?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>
修改后重新运行项目,类型转换的问题就会解决,你可以正常调用GlobalVars里的方法了。
内容的提问来源于stack exchange,提问作者rapture
相关产品推荐
相关产品推荐

