2017-10-21 115 views
0

我已經在這裏遵循的答案,但它沒有改變任何東西:異常MultiDexApplication不能從getApplication投射到類()

exception android.support.multidex.MultiDexApplication cannot be cast class

我有延伸MultiDexApplication一個MyApplication的類。在我的主要活動中,我調用getApplication()並將其轉換爲(MyApplication)。我的清單的應用程序的名稱設置爲.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> 

所有MyApplication:

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(); 
+0

你應該和你的代碼示例中,指示該評論一起提供完整的堆棧跟蹤崩潰發生。 – dominicoder

+0

增加了這兩個東西,謝謝 – Michael

+0

雖然沒有什麼突出的問題,這裏有許多可疑的事情:你的應用程序類擴展MultiDexApplication,所以它不應該(不)需要調用'MultiDex.install(this)',更別提_twice_了。你的應用程序不應該跟蹤當前的活動。由於Application是應用程序上下文,因此將變量'mAppContext'設置爲'getApplicationContext()'是沒有意義的。你的Activity擴展了一個名爲'AndroidApplication'的類?使用你的調試器,在它崩潰的那一行中斷,並檢查'getApplication()'的值。 – dominicoder

回答

0
MyApplication.getInstance(). getDefaultTracker(); 
+0

做到了這一點,現在我得到了一個N​​ullPointerException而不是 – Michael

0

這一切都表明您的自定義應用程序未被設置爲應用程序的應用程序,這意味着您的清單有問題。

仔細檢查你的清單,並確保你正在編輯正確的清單(可能有多種不同的風格,特別是因爲你有一個tools:replace標記),並確保你的MyApplication類正確。

如果沒有發現問題,發表您的完整清單(S)和居住地所在的文件夾/口味。

+0

我添加了完整的清單。我檢查了合併的清單,它有一個警告,工具:替換名稱和圖標是不需要的,所以刪除這些和只剩下的標籤。合併後的清單將這個與應用程序標籤匹配,其他清單中我在其他模塊中提供的任何內容都不會對合並的任何內容做出貢獻。我也做了一個新的項目,將所有內容都作爲一個模塊進行,只有這個清單和錯誤仍然存​​在。我通過使MyApplication擴展Application來獲得相同的轉換錯誤,但它仍然表示MultiDexApplication轉換錯誤,但不是Application,這很奇怪。 – Michael

+0

我也做了一個基本的新項目,只用一個MyApplication擴展MultiDexApplication並且只有一個活動,在這種情況下,它正確地調用MyApplication類。 – Michael

+0

嘗試了一堆東西后,從我的gradle文件中刪除編譯'com.fyber:fyber-sdk:8.0.1'修復了問題 – Michael

相關問題