我已经按照这里的答案,但它并没有改变任何东西:
exception android.support.multidex.MultiDexApplication cannot be cast class
我有一个MyApplication类,用于扩展MultiDexApplication。在我的主要 Activity 中,我调用getApplication()并将其强制转换为(MyApplication)。我的 list 的应用程序名称设置为.MyApplication。这是在Android Studio中。任何帮助,将不胜感激。谢谢!
错误:
FATAL EXCEPTION: main
Process: com.gametest.gametest123, PID: 4804
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gametest.gametest123/com.gametest.gametest123.GamesActivity}: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.gametest.gametest123.MyApplication
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access$800(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.gametest.gametest123.MyApplication
at com.gametest.gametest123.GamesActivity.onCreate(GamesActivity.java:124)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access$800(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.gametest.gametest123"
android:versionCode="33"
android:versionName="3.0.7" >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="26" />
<!-- Only this application can receive the messages and registration result -->
<permission android:name="com.gametest.gametest123.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.gametest.gametest123.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Generic permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- calldorado Permissions-->
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="22" ><!--Calldorado XML analyzer inserted the attribute android:maxSdkVersion="22" in order to make the code compatible with the Google Play store-->
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
android:maxSdkVersion="22" ><!--Calldorado XML analyzer inserted the attribute android:maxSdkVersion="22" in order to make the code compatible with the Google Play store-->
</uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
/>
<application
android:icon="@drawable/icon"
android:label="@string/SHORT_APP_NAME"
android:name=".MyApplication"
tools:replace="android:label">
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.gametest.gametest123" />
</intent-filter>
</receiver>
<receiver android:name="com.gametest.gametest123.LocalNotification"></receiver>
<receiver android:name="com.gametest.gametest123.CloseApp"></receiver>
<service android:name="com.gametest.gametest123.GCMIntentService" />
<activity
android:name="com.gametest.gametest123.activities.ViewApp"
android:label="@string/SHORT_APP_NAME"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
/>
<activity
android:name="com.gametest.gametest123.LoadViewApp"
android:label="@string/SHORT_APP_NAME"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
/>
<activity
android:name="com.gametest.gametest123.activities.OfferPage"
android:label="@string/SHORT_APP_NAME"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
/>
<activity
android:name="com.gametest.gametest123.discover.DiscoverApps"
android:label="@string/TITLESTRING_WHATSNEW"
android:screenOrientation="portrait"
/>
<activity
android:name="com.gametest.gametest123.activities.ChooseMailClient"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
/>
<activity
android:name="com.tapjoy.TJAdUnitActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity
android:name="com.tapjoy.mraid.view.ActionHandler"
android:configChanges="orientation|keyboardHidden|screenSize" />
<activity
android:name="com.tapjoy.mraid.view.Browser"
android:configChanges="orientation|keyboardHidden|screenSize" />
<activity
android:name="com.tapjoy.TJContentActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/SHORT_APP_NAME"
tools:replace="android:theme"/>
<!-- TODO: Fix -->
<!--android:label="@string/app_name" /-->
<activity android:name="com.fyber.ads.ofw.OfferWallActivity"
android:configChanges="orientation|screenSize" />
<!-- The GAMES activity -->
<activity
android:name="com.gametest.gametest123.GamesActivity"
android:label="@string/SHORT_APP_NAME"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<provider
android:authorities="com.facebook.app.FacebookContentProvider1730237540533882"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
<!-- Optionally, register AnalyticsReceiver and AnalyticsService to support background
dispatching on non-Google Play devices -->
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
android:exported="false"/>
<!-- Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
installation campaign reporting -->
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
</application>
</manifest>
我的应用程序:
package com.gametest.gametest123;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class MyApplication extends MultiDexApplication {
private Tracker mTracker;
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;
}
/**
* 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.string.ga_trackingId);
}
return mTracker;
}
}
GamesActivity直到崩溃所在的行:
public class GamesActivity extends AndroidApplication implements DownloadAndParseJsonAppTask.DownloadAndParseJsonInterface, DialogInterface.OnCancelListener, PlayGames {
private int mPresentNumber = 0;
public ActionResolverAndroid actionResolverAndroid;
public PlayGames mPlayGames;
private boolean userIsWaitingForConnection = false;
// Array to store all button elements initialized with 25 false elements (boolean is false by default, Boolean is null)
private boolean[] mDoorsArray = new boolean[25];
private DownloadAndParseJsonAppTask mTask;
private Chartboost chartboost;
// Loading data properties
public ProgressDialog mLoadingDialog = null;
public ArrayList<JsonData> mArrayJsonData;
public String mLastModified = null;
public Intent ofwIntent;
public static final int REQUEST_CODE = 102;
// Client used to interact with Google APIs
// private GoogleApiClient mGoogleApiClient;
GameHelper gameHelper;
public boolean loadUrl = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getADID();
AndroidPermissions.check(this)
.permissions(Manifest.permission.READ_PHONE_STATE)
.hasPermissions(new Checker.Action0() {
@Override
public void call(String[] permissions) {
// do something..
}
})
.noPermissions(new Checker.Action1() {
@Override
public void call(String[] permissions) {
// do something..
ActivityCompat.requestPermissions(GamesActivity.this
, new String[]{Manifest.permission.READ_PHONE_STATE}
, REQUEST_CODE);
}
})
.check();
// Load Google Analytics
//THIS IS WHERE THE APP CRASHES
((MyApplication) getApplication()).getDefaultTracker();
最佳答案
MyApplication.getInstance(). getDefaultTracker();
关于java - 无法从getApplication()将异常MultiDexApplication强制转换为类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46859560/