1. 程式人生 > >Android 有時環信初始化失敗,建立applocation失敗

Android 有時環信初始化失敗,建立applocation失敗

  java.lang.RuntimeException: Unable to create application com.wlkj.ciyun.sj.config.MyApp: java.lang.NullPointerException
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4349)
        at android.app.ActivityThread.access$1500(ActivityThread.java:135)
        at android.app
.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5019) 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:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException
at com.hyphenate.chat.a.b.a(Unknown Source) at com.hyphenate.chat.a.b.a(Unknown Source) at com.hyphenate.chat.EMClient.init(Unknown Source) at com.hyphenate.easeui.EaseUI.init(EaseUI.java:118) at com.wlkj.ciyun.sj.config.MyApp.onCreate(MyApp.java:50) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4346)

這裡寫圖片描述

實際上這個問題是因為Android工程方法數量超過了65536的限制
  下面給出解決方法:   
  第一步:
    在gradle的配置檔案中新增依賴 compile 'com.android.support:multidex:1.0.1'
  第二步:
    新增 multiDexEnabled true
    android {
      ...
      defaultConfig {
         ...
         multiDexEnabled true
         ...
       }
     }
  第三步:
    在工程的MainApplication中重寫  attachBaseContext() 方法
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
也可以直接在Application的onCreate方法中super.OnCreate();方法前加一句話MultiDex.install(this)就可以了