问题描述
当我尝试将目标SDK升级到29时,出现以下崩溃
Caused by: android.view.InflateException: Binary XML file line #23 in package:layout/abc_screen_simple_overlay_action_mode: Binary XML file line #23 in cpackage:layout/abc_screen_simple_overlay_action_mode: Error inflating class androidx.appcompat.widget.FitwindowsFrameLayout
Caused by: android.view.InflateException: Binary XML file line #23 in com.primedia.apartmentguide.debug:layout/abc_screen_simple_overlay_action_mode: Error inflating class androidx.appcompat.widget.FitwindowsFrameLayout
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Field.get(java.lang.Object)' on a null object reference
我已经升级了项目中的所有依赖项,但是没有用,当我寻找解决方案时,人们会说我需要升级Calligraphy3,但是我根本不在项目中使用此库。
这仅发生在Android 10中,在其他版本的android中也能很好地工作。
解决方法
我也遇到过类似的错误。我找到了答案 here。不确定到底是什么问题,但我想这是由于字体造成的。我做了以下事情:
- 添加新的依赖项
implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
- 添加了字体文件和部分代码,在出现问题时将替换为默认样式
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory2(getLayoutInflater(),new IconicsLayoutInflater2(getDelegate()));
super.onCreate(savedInstanceState);
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("font/TimesNewRoman.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
}
- 替换返回上下文的类
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
}
到
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}