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

时间:2016-02-26 18:10:53

标签: java android gradle

我有问题,我的应用生成此异常,我不明白。 我在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());

}

文件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>

7 个答案:

答案 0 :(得分:5)

我认为你应该将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

答案 1 :(得分:3)

您应该从Application扩展MultidexApplication课程,并在明文中使用它,而不是直接使用android.support.multidex.MultiDexApplication

答案 2 :(得分:2)

我遇到同样的问题,原因是您在清单中设置了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>

我的申请类

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

}

这就是为什么当我调用此方法(初始化应用程序实例)时它会崩溃,它会尝试将MultiDexApplication转换为MyApplication

MyApplication application = (MyApplication)getApplication();

所以要解决此问题,只需将您的清单属性名称更改为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 

答案 3 :(得分:0)

只需删除该行

即可
 android:name="android.support.multidex.MultiDexApplication"  

从清单文件中输入您的应用程序名称。即

 android:name="com.yourAppNamae.activity.YourApplicationName"

这对我有用

答案 4 :(得分:0)

要响应本机:

// Function Prototypes
void remove_all_chars(char *, char *);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) { //Function parameters allow for CLI arguments

    printf("%s\n", argv[1]);
    printf("%s\n", argv[2]);
    char *charTest2 = *argv[2];
    char *strTest = strdup(argv[1]);
    //char strTest[20];
    //char charTest[20];
    //strncpy(strTest, argv[1], 10);
    //strTest[0] = *argv[1];
    remove_all_chars(strTest, charTest2);
    printf("%s", strTest);

    return 0;

}


void remove_all_chars(char *str, char *c) {
    char *pr = str, *pw = str;
    while (*pr) {
        *pw = *pr++;
        pw += (*pw != c);
    }
    *pw = '\0';
}

答案 5 :(得分:0)

这里有几种正确的解决方案。但是,如果您使用的是react-native> = 0.60或使用androidx,则可以使用以下解决方案:

// MainApplication

...
import androidx.multidex.MultiDexApplication;

...    

public class MainApplication extends MultiDexApplication implements ReactApplication {

    ...
}

答案 6 :(得分:0)

添加到@Chandan答案中,实际上将原始应用android:name保留在AndroidManifest.xml可以使其正常工作(至少在我的案例中,RN> 0.60并使用Androidx)。