From 02a26c1a85bdf048ccfd1487e48d5b5aada48d4d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 22 May 2021 13:19:40 +0200 Subject: [PATCH] adding a crashfix --- .../pro/activities/MainActivity.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt index 749d64c3..7b1abda4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt @@ -138,7 +138,7 @@ class MainActivity : SimpleActivity() { override fun onPrepareOptionsMenu(menu: Menu?): Boolean { val favorites = config.favorites - val currentFragment = getCurrentFragment() + val currentFragment = getCurrentFragment() ?: return true menu!!.apply { findItem(R.id.search).isVisible = currentFragment is ItemsFragment @@ -163,6 +163,10 @@ class MainActivity : SimpleActivity() { } override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (getCurrentFragment() == null) { + return true + } + when (item.itemId) { R.id.go_home -> goHome() R.id.go_to_favorite -> goToFavorite() @@ -232,7 +236,7 @@ class MainActivity : SimpleActivity() { override fun onQueryTextChange(newText: String): Boolean { if (isSearchOpen) { - getCurrentFragment().searchQueryChanged(newText) + getCurrentFragment()?.searchQueryChanged(newText) } return true } @@ -337,7 +341,7 @@ class MainActivity : SimpleActivity() { main_view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { if (isSearchOpen) { - getCurrentFragment().searchQueryChanged("") + getCurrentFragment()?.searchQueryChanged("") searchMenuItem?.collapseActionView() } } @@ -436,23 +440,23 @@ class MainActivity : SimpleActivity() { } private fun goHome() { - if (config.homeFolder != getCurrentFragment().currentPath) { + if (config.homeFolder != getCurrentFragment()!!.currentPath) { openPath(config.homeFolder) } } private fun showSortingDialog() { - ChangeSortingDialog(this, getCurrentFragment().currentPath) { + ChangeSortingDialog(this, getCurrentFragment()!!.currentPath) { (getCurrentFragment() as? ItemsFragment)?.refreshItems() } } private fun addFavorite() { - config.addFavorite(getCurrentFragment().currentPath) + config.addFavorite(getCurrentFragment()!!.currentPath) } private fun removeFavorite() { - config.removeFavorite(getCurrentFragment().currentPath) + config.removeFavorite(getCurrentFragment()!!.currentPath) } private fun toggleFilenameVisibility() { @@ -482,7 +486,7 @@ class MainActivity : SimpleActivity() { favorites.forEachIndexed { index, path -> val visiblePath = humanizePath(path).replace("/", " / ") items.add(RadioItem(index, visiblePath, path)) - if (path == getCurrentFragment().currentPath) { + if (path == getCurrentFragment()!!.currentPath) { currFavoriteIndex = index } } @@ -493,12 +497,12 @@ class MainActivity : SimpleActivity() { } private fun setAsHome() { - config.homeFolder = getCurrentFragment().currentPath + config.homeFolder = getCurrentFragment()!!.currentPath toast(R.string.home_folder_updated) } private fun changeViewType() { - ChangeViewTypeDialog(this, getCurrentFragment().currentPath, getCurrentFragment() is ItemsFragment) { + ChangeViewTypeDialog(this, getCurrentFragment()!!.currentPath, getCurrentFragment() is ItemsFragment) { getAllFragments().forEach { it?.refreshItems() } @@ -543,7 +547,7 @@ class MainActivity : SimpleActivity() { return } - if (getCurrentFragment().breadcrumbs.childCount <= 1) { + if (getCurrentFragment()!!.breadcrumbs.childCount <= 1) { if (!wasBackJustPressed && config.pressBackTwice) { wasBackJustPressed = true toast(R.string.press_back_again) @@ -554,8 +558,8 @@ class MainActivity : SimpleActivity() { finish() } } else { - getCurrentFragment().breadcrumbs.removeBreadcrumb() - openPath(getCurrentFragment().breadcrumbs.getLastItem().path) + getCurrentFragment()!!.breadcrumbs.removeBreadcrumb() + openPath(getCurrentFragment()!!.breadcrumbs.getLastItem().path) } } @@ -628,7 +632,7 @@ class MainActivity : SimpleActivity() { private fun getAllFragments(): ArrayList = arrayListOf(items_fragment, recents_fragment) - private fun getCurrentFragment(): MyViewPagerFragment { + private fun getCurrentFragment(): MyViewPagerFragment? { val showTabs = config.showTabs val fragments = arrayListOf() if (showTabs and TAB_FILES != 0) { @@ -639,7 +643,7 @@ class MainActivity : SimpleActivity() { fragments.add(recents_fragment) } - return fragments[main_view_pager.currentItem] + return fragments.getOrNull(main_view_pager.currentItem) } private fun checkWhatsNewDialog() {