前言:
进来公司没啥事情做,就想着把公司以前的项目适配安卓Q以及android.support升级AndroidX 。这一升级,就遇到很对问题,程序一打开就直接奔溃,打开日志开头就遇到一个从未遇到过的问题:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{xxx.MainActivity}:
android.view.InflateException: Binary XML file line #17
in ir.mahdi.circulars:layout/abc_screen_simple: Binary XML file line #17
in ir.mahdi.circulars:layout/abc_screen_simple:
Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout
转头一想,明明在安卓P及以下都能完美运行代码,怎么到了Q就不行了,然后查了下百度,恩 好家伙果然了一个相同问题的小伙伴:链接但是这个哥们没给出答案直接让回退到28,我想了想这怎么能行呢,于是就疯狂在网上找个答案,果然在Stackoverflow网站上找到了传送门。
简单看了下解决方案:
在项目build.gradle中添加依赖
implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
可是一运行还是会遇到奔溃怎么回事呢,发现这个库呢是针对使用了自定义字体的,结合自己的项目发现确实使用了自定义字体,所以才会造成升级了Q后出现上文的那个错,然后找到这个库的GitHub地址根据操作:
1.在你项目的Application的onCreate()中初始化:
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("你的字体")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
2.在你的BaseActivity中attachBaseContext的方法中
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}
然后你就可以解决这个问题了,希望能帮助到遇到此问题的同行。
下一步准备解决升级Q后的PreferenceManager.getDefaultSharedPreferences(Context context)
方法被弃用的问题。
学无止境当个快乐的小菜鸟。