java 异常 android.support.multidex.MultiDexApplication 不能被强制转换类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35658796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exception android.support.multidex.MultiDexApplication cannot be cast class
提问by David Hackro
I have a problem, my app generate this exception,and i don't understand. I have implement the multiDexEnabled in my build.gradle
我有一个问题,我的应用程序生成了这个异常,我不明白。我在 build.gradle 中实现了 multiDexEnabled
Caused by: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.example.AnalyticsApplication
My Class Java
我的课程 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());
}
}
file Gradle
文件摇篮
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>
采纳答案by Dan Cantir
I think you should extend the AnalyticsApplication
class into your own class, like this:
我认为您应该将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());
}
}
After this, you must change your AndroidManifest.xml
file to this:
在此之后,您必须将AndroidManifest.xml
文件更改为:
<application
android:name="path.to.YourApplicationName"
...
Please, check this link for more information: http://developer.android.com/reference/android/support/multidex/MultiDex.html
请查看此链接以获取更多信息:http: //developer.android.com/reference/android/support/multidex/MultiDex.html
回答by Mina Fawzy
I have the same issue this cause of you set android:name="android.support.multidex.MultiDexApplication"
in your manifest so your application call is type of MultiDexApplication
我有同样的问题,你android:name="android.support.multidex.MultiDexApplication"
在清单中设置了这个原因,所以你的应用程序调用是类型MultiDexApplication
<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>
my application class
我的应用类
public class MyApplication extends Application {
public enum TrackerName {
APP_TRACKER, // tracker used only in this app
GLOBAL_TRACKER, // tracker used by all the apps from a company . eg: roll-up tracking.
ECOMMERCE_TRACKER, // tracker used by all ecommerce transactions from a company .
}
public HashMap<TrackerName, Tracker> mTrackers = new HashMap<>();
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = (trackerId == TrackerName.APP_TRACKER)?analytics.newTracker(Property_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId , tracker);
}
return mTrackers.get(trackerId);
}
private Activity mCurrentActivity = null;
public void setCurrentActivity(Activity mCurrectActivity) {
this.mCurrentActivity = mCurrectActivity;
}
public Activity getCurrentActivity() {
return mCurrentActivity;
}
private static Context mAppContext;
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
mInstance = this;
this.setAppContext(getApplicationContext());
}
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
public static MyApplication getInstance() {
return mInstance;
}
public static Context getAppContext() {
return mAppContext;
}
public void setAppContext(Context mAppContext) {
this.mAppContext = mAppContext;
}
}
this why when I call this method (initialize application instance ) it crash , it try cast MultiDexApplication
to MyApplication
这就是为什么当我调用此方法(初始化应用程序实例)时它会崩溃,它会尝试强制MultiDexApplication
转换为MyApplication
MyApplication application = (MyApplication)getApplication();
so to fix this issue just change your manifest attribute name to MyApplication
所以要解决这个问题,只需将清单属性名称更改为 MyApplication
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication"> // add path to your application class here
回答by Geralt_Encore
You should extend your Application
class from MultidexApplication
and use it in manifest instead of using android.support.multidex.MultiDexApplication
directly.
您应该扩展您的Application
类MultidexApplication
并在清单中使用它,而不是android.support.multidex.MultiDexApplication
直接使用它。
回答by Matheus Piscioneri
To React Native:
反应原生:
// MainApplication
...
import android.content.Context;
import android.support.multidex.MultiDex;
...
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(getBaseContext());
}
}
回答by Cory Robinson
There are several correct solutions here. But, if you're using react-native >= 0.60
or you're using androidx
then this is the solution:
这里有几个正确的解决方案。但是,如果您使用的是 react-native >=0.60
或者您正在使用,androidx
那么这就是解决方案:
// MainApplication
...
import androidx.multidex.MultiDexApplication;
...
public class MainApplication extends MultiDexApplication implements ReactApplication {
...
}
回答by Chandan Kumar
Just remove that line
只需删除该行
android:name="android.support.multidex.MultiDexApplication"
from manifest file and put your Application name . i.e
从清单文件中输入您的应用程序名称。IE
android:name="com.yourAppNamae.activity.YourApplicationName"
That worked for me
那对我有用