Извините, если это глупый вопрос.
Я пытаюсь сделать это Глобальная переменная Android
Я добавил новый класс, но не могу вызвать методы внутри класса.Я получаю:
необратимых типов;не может привести 'android.app.Application' к 'com.app.android.appname.classname'
вот код внутри нового класса:
public class GlobalVars extends Application {
private static int lvl;
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
lvl = this.lvl;
}
}
мой манифест:
<?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>
затем в действии:
protected void select1 (View view) {
((GlobalVars) this.getApplication()).setLvl(1); <-------- error
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
Решено
в манифесте, имя должно быть определено.не добавить.изменил его на:
<?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>
теперь это работает.