空指针异常只发生在 KitKat 上

Null Pointer Exception only happens on KitKat

我的应用程序有一个小问题。在设置片段中,按下导航栏上的后退按钮或后退箭头将导致崩溃。这只发生在 API 19。我正在使用 Navigator 和 Settings androidX 组件。我确保我使用的是最新的一切,从 gradle 到 androidX 模块。我的最小 API 是 19,因为我希望一些旧设备可以与此应用程序一起使用。我完全不知道问题出在哪里,唯一的解决办法是忍受它(每次进入设置时重新启动应用程序)或将最小 API 级别提高到 21.

发生崩溃的一段 MainScreen 片段 [什么是 NullPointerException,我该如何解决][1] 没有帮助:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View?
{
    //crash happens here, but only when returning from settings
    val view = inflater.inflate(R.layout.fragment_main_screen, container, false) 
    setHasOptionsMenu(true)
    view.button_newEntry.setOnClickListener {
        view.findNavController().navigate(R.id.action_mainScreen_to_newEntry)
    }

    return view
}

似乎在主屏幕视图的第二个 inflation 期间失败,不知道为什么。在较新版本上工作正常。

编辑:根据要求,mainScreen.xml我也知道 nullPointerException 是什么 [什么是 NullPointerException,我该如何修复它?][1] 没有帮助:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.MainScreen">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/uploadProgressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/searchView" />

        <SearchView
            android:id="@+id/searchView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rw_items"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:isScrollContainer="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/uploadProgressBar"
            tools:listitem="@layout/item" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_newEntry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:background="#00FFFFFF"
            android:clickable="true"
            android:focusable="true"
            android:src="@android:drawable/ic_input_add"
            android:visibility="visible"
            app:backgroundTint="#3F51B5"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

完整错误信息:

    Process: sk.tuke.archivator, PID: 5329
    android.view.InflateException: Binary XML file line #22: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:621)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at sk.tuke.archivator.Fragments.MainScreen.onCreateView(MainScreen.kt:43)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:595)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
        at sk.tuke.archivator.Fragments.MainScreen.onCreateView(MainScreen.kt:43) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150) 
        at android.os.Handler.handleCallback(Handler.java:733) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.NullPointerException
        at android.widget.SearchView.onRtlPropertiesChanged(SearchView.java:1359)
        at android.view.View.resolvePadding(View.java:12407)
        at android.view.ViewGroup.resolvePadding(ViewGroup.java:5665)
        at android.view.View.initializeScrollbars(View.java:4239)
        at android.view.View.<init>(View.java:3934)
        at android.view.ViewGroup.<init>(ViewGroup.java:470)
        at android.widget.LinearLayout.<init>(LinearLayout.java:176)
        at android.widget.LinearLayout.<init>(LinearLayout.java:172)
        at android.widget.SearchView.<init>(SearchView.java:245)
        at java.lang.reflect.Constructor.constructNative(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:595) 
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:670) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
        at sk.tuke.archivator.Fragments.MainScreen.onCreateView(MainScreen.kt:43) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150) 
        at android.os.Handler.handleCallback(Handler.java:733) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) ```


  [1]: 

感谢 @LenaBru I managed to find out that the searchView was causing the crash. After quite some trial and error replacing searchView with androidx.appcompat.widget.SearchView 解决了这个问题。谢谢大家的帮助。

您的应用在 XMLline #22 问题是它找不到您的 SearchView 如果您将其注释掉或替换为其他内容,它应该可以工作。