0

Despite i followed some StackOverflow's tips, I can't get rid of this error:

Android.app.Application cannot be cast to com.dostalgia.airshootingrogatti.GlobalClass

I inserted GlobalClass in manifest file, as already said in S.O.

This is the Global Class Code:

public class GlobalClass extends AppCompatActivity {


    private int StatoContatore1;


    public int getStatoContatore1() {
        return StatoContatore1;
    }

    public void setStatoContatore1(int statoContatore1) {
        StatoContatore1 = statoContatore1;
    }
}

And this is the activity (Not the main) that calls and generate error

package com.dostalgia.airshootingrogatti;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Postazione1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_postazione1);

        final GlobalClass globalVariable = (GlobalClass) getApplicationContext();


    }
}

This is my Mainfest file

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Postazione1"></activity>
        <activity android:name=".GlobalClass"></activity>

    </application>

</manifest>

My goal is to use and manage global variables in different activities!

Thanks for your reply!

Funesto
  • 67
  • 1
  • 9

1 Answers1

2

My goal is to use and manage global variables in different activities!

You are welcome to carefully use static fields. This would not require you to try creating a custom Application subclass, as you appear to be attempting to do here.

That being said, if you really want to do what I think you are trying to do:

Step #1: Have GlobalClass extend android.app.Application, not AppCompatActivity

Step #2: Remove the <activity> element in the manifest for GlobalClass

Step #3: Add an android:name attribute to the <application> element in the manifest, pointing to your GlobalClass class (e.g., android:name="com.dostalgia.airshootingrogatti.GlobalClass")

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks! My Application is not crashing anymore. To be honest as I am at my first experience in programming I am using video-tutorial to achieve what It's in my mind, so I don't know at all what is the best way to do. I was also trying the database way, but It's quite difficult for me! – Funesto May 02 '17 at 16:28