0
votes

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsapp/com.example.newsapp.ui.MainActivity}: android.view.InflateException: Binary XML file line #25 in com.example.newsapp:layout/activity_main: Binary XML file line #25 in com.example.newsapp:layout/activity_main: Error inflating class fragment 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)

   class MainActivity : AppCompatActivity() {

lateinit var viewModel : NewsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val newsRepository = NewsRepository(ArticleDatabase(this))
    val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
    viewModel = ViewModelProvider(this, viewModelProviderFactory).get(NewsViewModel::class.java)
    bottomNavigationView.setupWithNavController(nav_host_fragment_container.findNavController())
}


<FrameLayout
    android:id="@+id/flFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nav_host_fragment_container"
        app:defaultNavHost="true"
        app:navGraph="@navigation/news_nav_graph"/>

</FrameLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:backgroundTint="@color/grey"
    app:menu="@menu/bottom_navigation_menu"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"/>
1

1 Answers

1
votes

You have two mistakes in the code

First, you name the listview and the listfiles with the same variable name

// look at that in these lines, both of them had the same variable name        
private lateinit var list: ListView
val list = dir.listFiles()

Second, the part which makes the exception in this line

val list = dir.listFiles() // dir.listFiles() returns null 

check this answer if you want to get a list of files in directory as I understand