'Resources$NotFoundException - Failed to open file '...fragment_list.xml': No such file or directory
I am learning MVVM and have run into an issue, and I am getting the following errors:
Resources$NotFoundException: File res/layout/fragment_list.xml from xml type layout resource ID #0x7f0b002f
and
Failed to open file '/data/data/software.genau.dogs/code_cache/.overlay/base.apk/res/layout/fragment_list.xml': No such file or directory
I know the file exists in my layout, and there are no errors or warnings listed for the fragment_list.xml file. In the design view, everything is laid out the way I want and it looks correct.
To try and correct the issue I've tried some of the basic things like clean project and rebuilding, and restarted the program (and my machine). Also, I've tried different ways to override fun onCreateView() with and without binding.
I would appreciate some help.
Here is the MVVM layout: pic from Android studio folder layout
Here is my code for ListFragment.kt:
package software.genau.dogs.view
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import software.genau.dogs.R
import software.genau.dogs.databinding.FragmentListBinding
import software.genau.dogs.viewmodel.ListViewModel
class ListFragment : Fragment() {
private lateinit var binding: FragmentListBinding
private lateinit var viewModel: ListViewModel
private val dogsListAdapter = DogsListAdapter(arrayListOf())
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this).get(ListViewModel::class.java)
viewModel.refresh()
binding.dogsList.apply {
layoutManager = LinearLayoutManager(context)
adapter = dogsListAdapter
}
observeViewModel()
}
fun observeViewModel() {
viewModel.dogs.observe(viewLifecycleOwner, Observer { dogs ->
dogs?.let {
binding.dogsList.visibility = View.VISIBLE
dogsListAdapter.updateDogList(dogs)
}
})
viewModel.dogsLoadError.observe(viewLifecycleOwner, Observer { isError ->
isError?.let {
binding.listError.visibility = View.VISIBLE
}
})
viewModel.loading.observe(viewLifecycleOwner, Observer { isLoading ->
isLoading?.let {
binding.progressBar.visibility = if (it) View.VISIBLE else View.INVISIBLE
if (it) {
binding.listError.visibility = View.INVISIBLE
binding.dogsList.visibility = View.INVISIBLE
}
}
})
}
}
Here is the code for fragment_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dogsList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/listError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_fragment_error_msg"
app:layout_constraintBottom_toBottomOf="@+id/dogsList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/dogsList" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/dogsList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/dogsList" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>
Here is MainActivity.kt:
package software.genau.dogs.view
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavAction
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import software.genau.dogs.R
import software.genau.dogs.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
navController = navHostFragment.navController
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController, null)
}
}
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/dog_navigation"
tools:layout="@layout/fragment_list" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Here are the errors from logcat:
2021-11-17 12:29:08.420 12809-12809/software.genau.dogs E/ware.genau.dog: Failed to open file '/data/data/software.genau.dogs/code_cache/.overlay/base.apk/res/layout/fragment_list.xml': No such file or directory
2021-11-17 12:29:08.420 12809-12809/software.genau.dogs D/AndroidRuntime: Shutting down VM
2021-11-17 12:29:08.425 12809-12809/software.genau.dogs E/AndroidRuntime: FATAL EXCEPTION: main
Process: software.genau.dogs, PID: 12809
android.content.res.Resources$NotFoundException: File res/layout/fragment_list.xml from xml type layout resource ID #0x7f0b002f
at android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:1264)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2426)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2402)
at android.content.res.Resources.getLayout(Resources.java:1252)
at android.view.LayoutInflater.inflate(LayoutInflater.java:530)
at software.genau.dogs.view.ListFragment.onCreateView(ListFragment.kt:35)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8018)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
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: java.io.FileNotFoundException: res/layout/fragment_list.xml
at android.content.res.AssetManager.nativeOpenXmlAsset(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:1092)
at android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:1248)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2426)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2402)
at android.content.res.Resources.getLayout(Resources.java:1252)
at android.view.LayoutInflater.inflate(LayoutInflater.java:530)
at software.genau.dogs.view.ListFragment.onCreateView(ListFragment.kt:35)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8018)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
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)
Solution 1:[1]
Not sure if this is the best way, but I solved my issue.
I renamed the fragment_list.xml
to fragment_list2.xml
then added a new fragment_list.xml
. Finally, I copied all the data from the original file to the new file.
The new fragment_list.xml
is now recognized and the program is running.
Solution 2:[2]
I resolved this issue by making a small change in the XML (code editor) e.g changing or adding a margin to any View in the layout file. Then I went to top menu Build -> Clean Project followed by Build -> Rebuild Project. I think what happened is Android Studio/Gradle lost track of the changes made to the file, so it is not included when recompiling as it thinks it is there unmodified. By making a small arbitrary change to the file Gradle sees it has been modified so includes the layout as expected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brian Ball |
Solution 2 | yoyo |