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

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(); 
을 : 그것은 충돌 라인에

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 최대

+0

당신은 위치를 나타내는 코드 샘플에서 코멘트와 함께 전체 스택 트레이스를 제공해야 충돌이 발생합니다. – dominicoder

+0

은 감사합니다. – Michael

+0

아무 문제가 없지만 많은 의심의 여지가 있습니다 : 응용 프로그램 클래스가 MultiDexApplication을 확장하므로'MultiDex.install (this)'을 호출 할 필요가 없습니다. 혼자만 두지 마라. 애플리케이션이 현재 활동을 추적해서는 안됩니다. 변수'mAppContext'를'getApplicationContext()'로 설정하는 것은'Application'이 어플리케이션 컨텍스트이기 때문에 무의미합니다. 귀하의 활동은'AndroidApplication'이라는 클래스를 확장합니까? 디버거를 사용하여 충돌이 발생한 행에서 중단하고'getApplication()'의 값을 검사하십시오. – dominicoder

답변

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

그런데 이제는 줄에서 NullPointerException이 발생합니다. – Michael

0

이 모든 내용은 사용자 지정 응용 프로그램이 응용 프로그램의 응용 프로그램으로 설정되어 있지 않음을 나타냅니다. 즉, 매니페스트에 문제가 있음을 나타냅니다.

매니페스트를 두 번 확인하고 올바른 매니페스트를 편집했는지 확인하십시오 (특히 tools:replace 태그가 있기 때문에 다른 맛이 여러 개일 수 있음). MyApplication 클래스가 올바르게 지정되었는지 확인하십시오. 문제를 찾을 수없는 경우

, 전체 매니페스트 (들)을 게시하고 그들이 살고 어떤 폴더/맛.

+0

전체 매니페스트를 추가했습니다. 병합 된 매니페스트를 확인한 결과 이름과 아이콘을 대체 할 필요가 없으므로 툴을 제거하고 레이블을 남기라는 경고가있었습니다. 병합 된 매니페스트는이 코드를 응용 프로그램 태그와 일치시키고, 다른 모듈에있는 다른 매니페스트는 병합 된 매니페스트에 아무 것도 기여하지 않습니다. 또한 모든 것을 하나의 모듈로 만들고이 매니페스트와 여전히 동일한 오류 만있는 새 프로젝트를 만들었습니다.MyApplication 응용 프로그램을 확장하여 동일한 캐스팅 오류가 발생하지만 여전히 응용 프로그램이 아니라 이상한 경우에도 MultiDexApplication 캐스팅 오류가 발생합니다. – Michael

+0

필자는 또한 MultiDexApplication과 하나의 액티비티를 확장 한 MyApplication으로 기본 새 프로젝트를 만들었습니다.이 경우에는 MyApplication 클래스를 올바르게 호출했습니다. – Michael

+0

많은 것들을 시도한 후, 내 'grad 파일에서'com.fyber : fyber-sdk : 8.0.1 '을 제거하면 문제가 해결되었습니다. – Michael