场景:
我正在尝试为应用程序设置 Firebase 消息传递,但是当我添加 classpath 'com.google.gms:google-services:3.0.0'
时, apply plugin: 'com.google.gms.google-services'
在正确的 gradle 文件中,以及 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 库。在 list 中我有:
<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 类(class)并添加了
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
并尝试启动该应用程序,但因我之前发布的错误而崩溃。之后我去了
WelcomeActivity
和 ThemeableACtivity
,我也这样做了。我添加了 attachBaseContext
.但我有同样的问题。
我该如何解决?
谢谢。
最佳答案
android:name="your.package.YourApplicationClass"
试试这个。这里将 yoru YourApplicationClass 扩展到 MultiDexApplication
YourApplicationClass extends MultiDexApplication
关于android - 试图设置 MultiDexApplication , ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487666/