Android to Unable to instantiate Application java.lang.ClassNotFoundException:

前端 未结 4 2129
梦毁少年i
梦毁少年i 2021-01-30 17:29

I am working on application which is host on android market. Sometimes (once a month ) I got a crash report:

Unable to instantiate application java.lang.C

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 18:05

    I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.

    public class MyApplication extends Application {
        private static MyApplication me;
    
        @Override
        public void onCreate() {        
            super.onCreate();
            me = this ;
    
        }
        public static MyApplication getInstance() {
             return me;
        }
    }
    

    Now I have used getApplication() like this

         MyApplication application = MyApplication.getInstance();
    

    insted of

         MyApplication application = (MyApplication) getApplication();
    

    I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it.
    Thanks,

提交回复
热议问题