当我迁移到 AndroidX 时,我得到 java.lang.RuntimeException: Unable to start activity ComponentInfo

When I migrated to AndroidX I am getting java.lang.RuntimeException: Unable to start activity ComponentInfo

最近我将应用程序推送到上线,但它无法在 android 9 及更高版本上运行。所以,我升级了 android studio 到 2020.3.1,更新了插件,迁移了代码到 AndroidX。从那时起,我面临以下问题。

这里是运行时的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.meltag.hil_distributor, PID: 7726
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meltag.hil_distributor/com.meltag.hil_distributor.MainActivity}: android.view.InflateException: Binary XML file line #23 in com.meltag.hil_distributor:layout/abc_screen_toolbar: Binary XML file line #23 in com.meltag.hil_distributor:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarOverlayLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: android.view.InflateException: Binary XML file line #23 in com.meltag.hil_distributor:layout/abc_screen_toolbar: Binary XML file line #23 in com.meltag.hil_distributor:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarOverlayLayout
 Caused by: android.view.InflateException: Binary XML file line #23 in com.meltag.hil_distributor:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarOverlayLayout
 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
    at uk.co.chrisjenx.calligraphy.ReflectionUtils.getValue(ReflectionUtils.java:29)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.createCustomViewInternal(CalligraphyLayoutInflater.java:203)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.access[=12=]0(CalligraphyLayoutInflater.java:20)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:302)
    at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:237)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:899)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
    at com.meltag.hil_distributor.MainActivity.onCreate(MainActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:7994)
    at android.app.Activity.performCreate(Activity.java:7978)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<!-- The main content view -->

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linear_lay1"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar_lay1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_gravity="bottom"
                android:background="@drawable/toolbar_gradient_bg"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ToolBarStyle">

                <RelativeLayout
                    android:id="@+id/relative_lay1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/Main_navBtn"
                        android:layout_width="48dp"
                        android:layout_height="48dp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="3dp"
                        android:padding="12dp"
                        android:src="@drawable/ic_menu"
                        tools:ignore="ContentDescription"/>

                    <TextView
                        android:id="@+id/tv_tittle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="18dp"
                        android:layout_marginLeft="60dp"
                        android:layout_marginStart="60dp"
                        android:text="Dashboard"
                        android:textAllCaps="true"
                        android:textColor="@android:color/white"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:visibility="visible"
                        tools:ignore="HardcodedText,RtlHardcoded"/>

                </RelativeLayout>

            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.AppBarLayout>
        <!-- Real content goes here -->
        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fitsSystemWindows="true"/>

        <!--<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:background="@drawable/ic_footer_login"/>-->

    </LinearLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <!-- The navigation drawer -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.meltag.hil_distributor.fragments.FragmentDrawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

这是我的 onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final FragmentDrawer drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    Toolbar toolbar = findViewById(R.id.toolbar);
    title = toolbar.findViewById(R.id.tv_tittle);
    toolbar.findViewById(R.id.Main_navBtn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            drawerFragment.openDrawerFrag();
        }
    });
    toolbar.setNavigationIcon(null);
    toolbar.setContentInsetsAbsolute(0, 0);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    userPref = new AppPreferences(this);

    DrawerLayout mDrawerLayout = findViewById(R.id.drawer_layout);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, mDrawerLayout);
    drawerFragment.setDrawerListener(MainActivity.this);

    displayView(0);
}

我不知道如何解决这个问题。有什么建议吗?

只需清理您的项目并重建您的项目。

您使用的 Calligraphy 版本已过时,您需要更新 Calligraphy 3 以支持 android 10 及更高版本。 参考这个link:
https://github.com/InflationX/Calligraphy