无法将android.support.multidex.MultiDexApplication异常强制转换为类

问题描述

我认为您应该将AnalyticsApplication班级扩展到自己的班级,如下所示:

public class YourApplicationName extends AnalyticsApplication {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.activity_splash);

          //Analitycs
          AnalyticsApplication application = (AnalyticsApplication) getApplication();
          mTracker = application.getDefaultTracker();
          mTracker.setScreenName("Splash");
          mTracker.send(new HitBuilders.ScreenViewBuilder().build());
     }

     // Here you will enable Multidex
     @Override
     protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);
          MultiDex.install(getBaseContext());
     }

}

之后,您必须将AndroidManifest.xml文件更改为此:

<application
     android:name="path.to.YourApplicationName"
     ...

请检查此链接获取更多信息:http ://developer.android.com/reference/android/support/multidex/MultiDex.html

解决方法

我有一个问题,我的应用生成了此异常,我听不懂。我已经在我的build.gradle中实现了multiDexEnabled

  Caused by: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.example.AnalyticsApplication

我的班级Java

public class Splash extends Activity {

 private Tracker mTracker;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_splash);

  //Analitycs
  AnalyticsApplication application = (AnalyticsApplication) getApplication();
  mTracker = application.getDefaultTracker();
  mTracker.setScreenName("Splash");
  mTracker.send(new HitBuilders.ScreenViewBuilder().build());

}

文件摇篮

 defaultConfig {
    multiDexEnabled true
}

Manifest.xml

<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".Splash">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter></activity>
</application>

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”