0

I am using a tutorial provided by Google to implement Analytics in my app but something I possibly did wrong that causes the app to crash with java.lang.ClassCastException

This is what Google provided:

// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

This is the changes I made because I am using a Fragment

// This is where I get the error
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
mTracker = application.getDefaultTracker();

UPDATE : The error happens at this line:

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

This is my LogCat

FATAL EXCEPTION: main
Process: com.incorp.labs.appname, PID: 14095
java.lang.ClassCastException: android.app.Application cannot be cast to com.incorp.labs.appname.Helper.AnalyticsTracker
  at com.incorp.labs.appname.OneFragment.onCreateView(OneFragment.java:126)

UPDATE 2 : This is the Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.incorp.labs.appname">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/newlogops"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".Splash"
        android:screenOrientation="portrait" />
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".OneFragment"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".TwoFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".Feedback"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".FourFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".SplashTimer"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

    <service android:name=".FirebaseMessagingService">
        <intent-filter>                     
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
             
        </intent-filter>
    </service>

    <activity android:name=".AboutActivity"></activity>
</application>

Thorvald
  • 3,424
  • 6
  • 40
  • 66

3 Answers3

4

Just change this :-

AnalyticsTracker application = (AnalyticsTracker) getContext().getApplicationContext();

to this :-

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

It's one of those obvious errors that happen with the best of us!

Rohan Stark
  • 2,346
  • 9
  • 16
2

In addition to implementing the extends Application class, you need to tell Android system which class to use as an Application class. To do so, set the name of your application class in manifest:

<application
        android:name="your.package.AnalyticsApplication"
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
1

I think you're trying to use a Context object (getApplicationContext()) as an application object.

Try this: EDIT

AnalyticsApplication application = (AnalyticsApplication) getActivity().getApplication();

From your fragment you can call the activity that contains the fragment. When you get the activity, you can use the getApplication() function as in the example.

I hope it helps.

EDIT 2

As seen in this answer: https://stackoverflow.com/a/35905601/4336354 from CommonsWare user.

Your AndroidManifest.xml file does not contain your custom Application subclass, so Android is using the default one (android.app.Application).

Add an android:name="auc.games2.Analytics.AnalyticsApplication" attribute to your element.