尝试设置MultiDexApplication,ClassCastException

时间:2016-09-14 10:01:54

标签: android crash android-multidex

场景:

我尝试将Firebase Messaging设置为应用程序,但是当我在正确的gradle文件和classpath 'com.google.gms:google-services:3.0.0'文件中添加apply plugin: 'com.google.gms.google-services'google-services.json时,我尝试启动应用程序,但它崩溃了:

java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.res.Resources.getResourcePackageName(int)' on a null object reference 

在SO中阅读了很多内容,我读到了一些可能因65K方法限制而发生的评论。所以我试着"实施"应用程序中的multidex。

我按照android指南中的几个步骤进行操作 - > https://developer.android.com/studio/build/multidex.html

问题是,现在我收到了这个错误:

09-14 11:42:36.371 31466-31466/es.in2.otr.app.im E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: es.in2.otr.app.im, PID: 31466
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{es.in2.otr.app.im/es.in2.otr.app.im.app.WelcomeActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to es.in2.otr.app.im.app.ImApp
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                    Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to es.in2.otr.app.im.app.ImApp
                                                                       at es.in2.otr.app.im.app.ThemeableActivity.onCreate(ThemeableActivity.java:40)
                                                                       at es.in2.otr.app.im.app.WelcomeActivity.onCreate(WelcomeActivity.java:98)
                                                                       at android.app.Activity.performCreate(Activity.java:6876)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:158) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

在gradle文件中,我设置了multiDexApplication true并编译了de multidex库。

在清单中我有:

<application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher_in2"
        android:label="@string/im_label"
        android:theme="@style/AppTheme"
        tools:node="merge"
        tools:replace="icon, label, android:allowBackup">
.....
</application>

我为android:name=".app.ImApp"

更改了android:name="android.support.multidex.MultiDexApplication"

然后我去了ImApp课程并添加了

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

并尝试启动应用程序,但因为我之前发布的错误而崩溃了。在此之后我去了WelcomeActivityThemeableACtivity,我也做了同样的事情。我添加了attachBaseContext

但我有同样的问题。

我该如何解决? 感谢。

1 个答案:

答案 0 :(得分:3)

android:name="your.package.YourApplicationClass"

试试这个。这里将yoru YourApplicationClass扩展为MultiDexApplication

YourApplicationClass extends MultiDexApplication