当前位置: 代码迷 >> Android >> 如果我实施谷歌分析活动崩溃(com.google.android.gms:play-services-analytics:10.0.1)
  详细解决方案

如果我实施谷歌分析活动崩溃(com.google.android.gms:play-services-analytics:10.0.1)

热度:60   发布时间:2023-08-04 10:56:59.0

我实现了谷歌分析,并在活动中实例化了当我打开它时它崩溃的类......我遵循了

在我的成绩文件中

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

分析应用程序文件

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;
  }
}

我还添加了 google-services.json 文件

在我的活动文件中

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]

来自android监视器的错误

 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)

您是否在清单文件中声明了您的应用程序名称?

<application
    android:name=".AnalyticsApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
   .................
   </application>
  相关解决方案