Android 10 : Crash for abc_screen_simple: Error inflating class

วันนี้มาพร้อมกับอีกหนึ่งปัญหาที่เกิดขึ้นกับ Android 10 ไม่สามารถรัน Activity ได้สำเร็จแล้วก็ Crash พร้อมกับขึ้น Logcat Error ว่า

java.lang.RuntimeException: Unable to start activity 
    
     android.view.InflateException: Binary XML file line #17 
     in ir.mahdi.circulars:layout/abc_screen_simple: 
         Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout

ซึ่ง Android 10 จะ Crash ตั้งแต่ก่อนสร้าง ContainView อีก ซึ่งพบว่ามีการกำหนด attachBaseContext ดังนี้

@Override
protected void attachBaseContext(Context newBase) {
 super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

โดย CalligraphyContextWrapper นี้ import มาจาก ‘uk.co.chrisjenx:calligraphy:2.3.0′ จากการสืบค้นเพิ่มเติมพบว่า Calligraphy2.x ใน github แจ้งหยุดการบำรุงรักษา พร้อมกับให้เราย้ายไปใช้ Calligraphy3 ตามลิงค์

แนวทางการแก้ไขปัญหานี้

คือการ upgrade Calligraphy 2.x to Calligraphy 3.x โดยมีขั้นตอนดังนี้

1) แก้ไข Dependency ใน build.gradle

เดิม

implementation 'uk.co.chrisjenx:calligraphy:2.3.0'

ไปเป็น

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'

2) แก้ไข Calligraphy Initial

เดิม

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/poppins_med.ttf")
.setFontAttrId(R.attr.fontPath)
.build());

เปลี่ยนเป็น

ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/poppins_med.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());

3) แก้ไข attachBaseContext

เดิม

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

เปลี่ยนเป็น

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}

แล้วลอง Build & Run ใหม่อีกครั้งครับ

You may also like...