0

My app has a small problem. In settings fragment, pressing the back button or back arrow on navBar will cause a crash. This only happens on API 19. I am using Navigator and Settings androidX components. I made sure I am using latest everything, from gradle to androidX modules. My minimum API is 19, because I expect some old devices to be used with this app. I am completely clueless as to where the problem might be and the only solutions are to either bear it (just restart the app every time you go into settings) or raise the min API level to 21.

A piece of MainScreen fragment where the crash occurs so [What is a NullPointerException, and how do I fix it][1] does not help:

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
}

Seems to fail during second inflation of the MainScreen view, no idea why. Works fine on newer versions.

Edit: As requested, mainScreen.xml Also I know what nullPointerException is so [What is a NullPointerException, and how do I fix it?][1] does not help:

<?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>

Full error message:

    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$2.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$2.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$2.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]: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it
Luk164
  • 657
  • 8
  • 22
  • 1
    Can you add your `R.layout.fragment_main_screen` xml? – Md. Asaduzzaman Jan 22 '20 at 17:06
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Md. Asaduzzaman Jan 22 '20 at 17:35
  • @Md.Asaduzzaman Added and no it does not. I know what the error means, I just have no idea where to find it. – Luk164 Jan 22 '20 at 20:38
  • 1
    if you comment the search view out, and the code associated with it, does it crash ? it says it can't inflate at line #22 and that falls on the search view – Lena Bru Jan 22 '20 at 21:43
  • 1
    @LenaBru That worked! I have no idea why but it worked! Post as solution and I will mark it. Now I just need to override the resource and drop search functionality for API 19. – Luk164 Jan 23 '20 at 11:15

2 Answers2

1

Your app falls on line #22 on the XML The issue is that it can't find your SearchView If you comment it out or replace it with something else, it should work.

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
0

Thanks to @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 solved the issue. Thank you everyone for your help.

Luk164
  • 657
  • 8
  • 22