-1

I implemented google analytics and in the activity i instantiate the class it crashes when i open it ... I followed the google docs on v4

In my Grade file

compile 'com.google.android.gms:play-services-analytics:10.0.1'

AnalyticsApplication File

package website.copyandpaste.bottombarnavigationwithnavigationdrawer;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

/**
 * This is a subclass of {@link Application} used to provide shared objects for this app, such as
 * the {@link Tracker}.
 */
public class AnalyticsApplication extends Application {

  private static GoogleAnalytics sAnalytics;
  private static Tracker sTracker;

  @Override
  public void onCreate() {
    super.onCreate();

    sAnalytics = GoogleAnalytics.getInstance(this);
  }

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
    if (sTracker == null) {
      sTracker = sAnalytics.newTracker(R.xml.global_tracker);
    }

    return sTracker;
  }
}

I have also added the google-services.json file

In my activity file

AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mTracker = application.getDefaultTracker();

        // [START screen_view_hit]
        Log.i(TAG, "Setting screen name: " + "Main");
        mTracker.setScreenName("Image~" + "Main");
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
        // [END screen_view_hit]

The error from android monitor

 Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to website.copyandpaste.bottombarnavigationwithnavigationdrawer.AnalyticsApplication
                                                                                                            at website.copyandpaste.bottombarnavigationwithnavigationdrawer.CategoryActivity.onCreate(CategoryActivity.java:40)
  • *I followed the* no, you didn't. You didn't do this: *Update your project's AndroidManifest.xml* (as your app still is using default `android.app.Application` instead custom one) – Selvin Feb 13 '17 at 11:16
  • I have added this in manifest already ' ' – Simeon Adedayo Feb 13 '17 at 11:18
  • No, it's not a problem with a permissions – Selvin Feb 13 '17 at 11:18
  • then what do you think is the problem....look at the error from my android monitor – Simeon Adedayo Feb 13 '17 at 11:20
  • Possible duplicate of [Custom global Application class breaks with "android.app.Application cannot be cast to"](http://stackoverflow.com/questions/10607392/custom-global-application-class-breaks-with-android-app-application-cannot-be-c) – Selvin Feb 13 '17 at 11:22
  • or duplicate of any of the question, here on SO, which may be returned with internet search: "ClassCastException: android.app.Application cannot be cast to AnalyticsApplication" ... **please learn how to use internet search** – Selvin Feb 13 '17 at 11:23

1 Answers1

0

Have you declared your application name in your manifest file?

<application
    android:name=".AnalyticsApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
   .................
   </application>
Piyush
  • 2,589
  • 6
  • 38
  • 77