2017-10-21 81 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

相关问题