java - 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.
解决方案
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();
推荐阅读
- javascript - 从 Axios Promise 存储数据以供多种用途
- javascript - 尝试使用 Fetch 在 JavaScript 中记录来自 REST API 的 JSON 响应
- html - 如何使用 Chome 和/或 Firefox 上的检查窗口找到网页元素的 innerText 和 outerHTML?
- google-bigquery - BigQuery DataStudio 集成不起作用
- python - 准牛顿方法实现无法正常工作
- push-notification - 推送通知在自定义应用程序中无法正常工作 [Rocket.chat]
- matlab - Matlab自回归神经网络开环与闭环
- azure - azure-sql 上的“无法连接到当前状态的数据库”是什么意思?
- r - 在 ggplot2 自定义贴标机中使用 bquote
- javascript - 我正在研究 react js 并使用 session storage 。当我打开一个新标签时,数据越来越清晰