【问题标题】:Error: incompatible types: Application cannot be converted to AnalyticsApplication.: Android Studio 2.3.2错误:不兼容的类型:应用程序无法转换为 AnalyticsApplication。:Android Studio 2.3.2
【发布时间】:2017-06-06 08:47:32
【问题描述】:

我将我的 Android Studio 更新到 2.3.2,之后 gradle 构建失败并出现以下错误:

error: incompatible types: Application cannot be converted to AnalyticsApplication.

我已尝试搜索它,但找不到任何相关内容。

build.gradle 文件:

android {
  compileSdkVersion 25
  buildToolsVersion "25.0.2"

defaultConfig {
    applicationId "com.xxx.xxx"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 17
    versionName "1.6.7"
    multiDexEnabled true
}

这里出错了,

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

        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPrefsEditor = sharedPrefs.edit();

        defineFields();

    } catch (Exception e) {
        e.printStackTrace();
    }

AnalyticApplication.java:

import android.app.Application;
import android.support.multidex.MultiDexApplication;

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 MultiDexApplication {
    private Tracker mTracker;

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

【问题讨论】:

  • 使用 build、gradle 文件更新您的问题
  • 还有你的应用程序类
  • @cricket_007 请检查更新后的问题。

标签: android android-application-class


【解决方案1】:

我看你用过

import android.support.multidex.MultiDexApplication;

正如你特别提到的将工作室版本更新到 2.3,我建议你看看Getting "package android.support.multidex does not exist" after upgrading to Android Studio 2.3

我不太确定,但这可能是您问题的解决方案,因为我自己在工作室升级后也遇到了类似的问题。 希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2021-04-27
    • 2017-10-03
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多