@@ -138,7 +138,7 @@ class MainActivity : SimpleActivity() {
138138
139139 override fun onPrepareOptionsMenu (menu : Menu ? ): Boolean {
140140 val favorites = config.favorites
141- val currentFragment = getCurrentFragment()
141+ val currentFragment = getCurrentFragment() ? : return true
142142
143143 menu!! .apply {
144144 findItem(R .id.search).isVisible = currentFragment is ItemsFragment
@@ -163,6 +163,10 @@ class MainActivity : SimpleActivity() {
163163 }
164164
165165 override fun onOptionsItemSelected (item : MenuItem ): Boolean {
166+ if (getCurrentFragment() == null ) {
167+ return true
168+ }
169+
166170 when (item.itemId) {
167171 R .id.go_home -> goHome()
168172 R .id.go_to_favorite -> goToFavorite()
@@ -232,7 +236,7 @@ class MainActivity : SimpleActivity() {
232236
233237 override fun onQueryTextChange (newText : String ): Boolean {
234238 if (isSearchOpen) {
235- getCurrentFragment().searchQueryChanged(newText)
239+ getCurrentFragment()? .searchQueryChanged(newText)
236240 }
237241 return true
238242 }
@@ -337,7 +341,7 @@ class MainActivity : SimpleActivity() {
337341 main_view_pager.addOnPageChangeListener(object : ViewPager .OnPageChangeListener {
338342 override fun onPageScrollStateChanged (state : Int ) {
339343 if (isSearchOpen) {
340- getCurrentFragment().searchQueryChanged(" " )
344+ getCurrentFragment()? .searchQueryChanged(" " )
341345 searchMenuItem?.collapseActionView()
342346 }
343347 }
@@ -436,23 +440,23 @@ class MainActivity : SimpleActivity() {
436440 }
437441
438442 private fun goHome () {
439- if (config.homeFolder != getCurrentFragment().currentPath) {
443+ if (config.homeFolder != getCurrentFragment()!! .currentPath) {
440444 openPath(config.homeFolder)
441445 }
442446 }
443447
444448 private fun showSortingDialog () {
445- ChangeSortingDialog (this , getCurrentFragment().currentPath) {
449+ ChangeSortingDialog (this , getCurrentFragment()!! .currentPath) {
446450 (getCurrentFragment() as ? ItemsFragment )?.refreshItems()
447451 }
448452 }
449453
450454 private fun addFavorite () {
451- config.addFavorite(getCurrentFragment().currentPath)
455+ config.addFavorite(getCurrentFragment()!! .currentPath)
452456 }
453457
454458 private fun removeFavorite () {
455- config.removeFavorite(getCurrentFragment().currentPath)
459+ config.removeFavorite(getCurrentFragment()!! .currentPath)
456460 }
457461
458462 private fun toggleFilenameVisibility () {
@@ -482,7 +486,7 @@ class MainActivity : SimpleActivity() {
482486 favorites.forEachIndexed { index, path ->
483487 val visiblePath = humanizePath(path).replace(" /" , " / " )
484488 items.add(RadioItem (index, visiblePath, path))
485- if (path == getCurrentFragment().currentPath) {
489+ if (path == getCurrentFragment()!! .currentPath) {
486490 currFavoriteIndex = index
487491 }
488492 }
@@ -493,12 +497,12 @@ class MainActivity : SimpleActivity() {
493497 }
494498
495499 private fun setAsHome () {
496- config.homeFolder = getCurrentFragment().currentPath
500+ config.homeFolder = getCurrentFragment()!! .currentPath
497501 toast(R .string.home_folder_updated)
498502 }
499503
500504 private fun changeViewType () {
501- ChangeViewTypeDialog (this , getCurrentFragment().currentPath, getCurrentFragment() is ItemsFragment ) {
505+ ChangeViewTypeDialog (this , getCurrentFragment()!! .currentPath, getCurrentFragment() is ItemsFragment ) {
502506 getAllFragments().forEach {
503507 it?.refreshItems()
504508 }
@@ -543,7 +547,7 @@ class MainActivity : SimpleActivity() {
543547 return
544548 }
545549
546- if (getCurrentFragment().breadcrumbs.childCount <= 1 ) {
550+ if (getCurrentFragment()!! .breadcrumbs.childCount <= 1 ) {
547551 if (! wasBackJustPressed && config.pressBackTwice) {
548552 wasBackJustPressed = true
549553 toast(R .string.press_back_again)
@@ -554,8 +558,8 @@ class MainActivity : SimpleActivity() {
554558 finish()
555559 }
556560 } else {
557- getCurrentFragment().breadcrumbs.removeBreadcrumb()
558- openPath(getCurrentFragment().breadcrumbs.getLastItem().path)
561+ getCurrentFragment()!! .breadcrumbs.removeBreadcrumb()
562+ openPath(getCurrentFragment()!! .breadcrumbs.getLastItem().path)
559563 }
560564 }
561565
@@ -628,7 +632,7 @@ class MainActivity : SimpleActivity() {
628632
629633 private fun getAllFragments (): ArrayList <MyViewPagerFragment ?> = arrayListOf (items_fragment, recents_fragment)
630634
631- private fun getCurrentFragment (): MyViewPagerFragment {
635+ private fun getCurrentFragment (): MyViewPagerFragment ? {
632636 val showTabs = config.showTabs
633637 val fragments = arrayListOf<MyViewPagerFragment >()
634638 if (showTabs and TAB_FILES != 0 ) {
@@ -639,7 +643,7 @@ class MainActivity : SimpleActivity() {
639643 fragments.add(recents_fragment)
640644 }
641645
642- return fragments[ main_view_pager.currentItem]
646+ return fragments.getOrNull( main_view_pager.currentItem)
643647 }
644648
645649 private fun checkWhatsNewDialog () {
0 commit comments