Вылетает приложение после удаления файла при переходе на главный фрагмент
Почему после удаления вылетает приложение при переходе на главный фрагмент?
вот логи:
2021-11-15 15:30:56.805 25103-25103/com.vinson.afpoffice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vinson.afpoffice, PID: 25103
java.lang.IllegalStateException: View com.google.android.material.textview.MaterialTextView{acbca21 VFED..C.. ...P.... 55,368-1025,491 #7f0900a5 app:id/dialog_yes} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at com.vinson.afpoffice.fragments.EditBoardFragment.onOptionsItemSelected$lambda-1(EditBoardFragment.kt:115)
at com.vinson.afpoffice.fragments.EditBoardFragment.$r8$lambda$HkEKWKoCHyt1vjKOCRqn5FSEUFQ(Unknown Source:0)
at com.vinson.afpoffice.fragments.EditBoardFragment$$ExternalSyntheticLambda2.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7496)
at android.view.View.performClickInternal(View.java:7473)
at android.view.View.access$3600(View.java:831)
at android.view.View$PerformClick.run(View.java:28632)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7876)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
вот код:
package com.vinson.afpoffice.fragments
import android.os.Bundle
import android.text.TextUtils
import android.text.format.DateFormat
import android.view.*
import android.widget.EditText
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.vinson.afpoffice.Model.Boards
import com.vinson.afpoffice.R
import com.vinson.afpoffice.databinding.FragmentEditBoardBinding
import com.vinson.afpoffice.viewModel.BoardsViewModel
import java.math.RoundingMode
import java.text.DecimalFormat
import java.util.*
class EditBoardFragment : Fragment() {
private val oldBoards by navArgs<EditBoardFragmentArgs>()
private lateinit var binding: FragmentEditBoardBinding
private val viewModel: BoardsViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
binding = FragmentEditBoardBinding.inflate(layoutInflater, container, false)
setHasOptionsMenu(true)
binding.fullName.setText(oldBoards.data.name)
binding.plan.setText(oldBoards.data.plan)
binding.plan1.setText(oldBoards.data.plan1)
binding.plan2.setText(oldBoards.data.plan2)
binding.plan3.setText(oldBoards.data.plan3)
binding.fact.setText(oldBoards.data.fact)
binding.fact1.setText(oldBoards.data.fact1)
binding.fact2.setText(oldBoards.data.fact2)
binding.fact3.setText(oldBoards.data.fact3)
binding.percentOfComp.text = oldBoards.data.comp
binding.percentOfComp1.text = oldBoards.data.comp1
binding.percentOfComp2.text = oldBoards.data.comp2
binding.percentOfComp3.text = oldBoards.data.comp3
binding.btnSaveBoard.setOnClickListener {
updateBoards(it)
}
return binding.root
}
private fun updateBoards(it: View?) {
val name = binding.fullName.text.toString()
val plan = binding.plan.text.toString()
val plan1 = binding.plan1.text.toString()
val plan2 = binding.plan2.text.toString()
val plan3 = binding.plan3.text.toString()
val fact = binding.fact.text.toString()
val fact1 = binding.fact1.text.toString()
val fact2 = binding.fact2.text.toString()
val fact3 = binding.fact3.text.toString()
val comp = binding.percentOfComp.text.toString()
val comp1 = binding.percentOfComp1.text.toString()
val comp2 = binding.percentOfComp2.text.toString()
val comp3 = binding.percentOfComp3.text.toString()
val d = Date()
val boardDate: CharSequence = DateFormat.format("d MMMM, yyyy ", d.time)
val data = Boards(
oldBoards.data.id,
name = name,
plan = plan,
plan1 = plan1,
plan2 = plan2,
plan3 = plan3,
fact = fact,
fact1 = fact1,
fact2 = fact2,
fact3 = fact3,
comp = comp,
comp1 = comp1,
comp2 = comp2,
comp3 = comp3,
date = boardDate.toString()
)
viewModel.updateBoards(data)
Navigation.findNavController(it!!).navigate(R.id.action_editBoardFragment_to_boardFragment)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.delete, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.menu_delete) {
val bottomSheet =
BottomSheetDialog(requireContext())
bottomSheet.setContentView(R.layout.dialog_delete)
val textviewYes = bottomSheet.findViewById<TextView>(R.id.dialog_yes)
val textviewNo = bottomSheet.findViewById<TextView>(R.id.dialog_no)
textviewYes?.setOnClickListener {
viewModel.deleteBoards(oldBoards.data.id)
Navigation.findNavController(it!!).navigate(R.id.action_editBoardFragment_to_boardFragment)
}
textviewNo?.setOnClickListener {
bottomSheet.dismiss()
}
bottomSheet.show()
}
return super.onOptionsItemSelected(item)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val plan = view.findViewById<EditText>(R.id.plan)
val fact = view.findViewById<EditText>(R.id.fact)
val resultTextView = view.findViewById<TextView>(R.id.percent_of_comp)
fact.addTextChangedListener {
if (!TextUtils.isEmpty(plan.text) && !TextUtils.isEmpty(fact.text)) {
val firstN = plan.text.toString().toDouble()
val secondN = fact.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView.text = it }
} else {
resultTextView.text = ""
}
}
plan.addTextChangedListener {
if (!TextUtils.isEmpty(plan.text) && !TextUtils.isEmpty(fact.text)) {
val firstN = plan.text.toString().toDouble()
val secondN = fact.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView.text = it }
} else {
resultTextView.text = ""
}
}
val plan1 = view.findViewById<EditText>(R.id.plan1)
val fact1 = view.findViewById<EditText>(R.id.fact1)
val resultTextView1 = view.findViewById<TextView>(R.id.percent_of_comp1)
fact1.addTextChangedListener {
if (!TextUtils.isEmpty(plan1.text) && !TextUtils.isEmpty(fact1.text)) {
val firstN = plan1.text.toString().toDouble()
val secondN = fact1.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView1.text = it }
} else {
resultTextView1.text = ""
}
}
plan1.addTextChangedListener {
if (!TextUtils.isEmpty(plan1.text) && !TextUtils.isEmpty(fact1.text)) {
val firstN = plan1.text.toString().toDouble()
val secondN = fact1.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView1.text = it }
} else {
resultTextView1.text = ""
}
}
val plan2 = view.findViewById<EditText>(R.id.plan2)
val fact2 = view.findViewById<EditText>(R.id.fact2)
val resultTextView2 = view.findViewById<TextView>(R.id.percent_of_comp2)
fact2.addTextChangedListener {
if (!TextUtils.isEmpty(plan2.text) && !TextUtils.isEmpty(fact2.text)) {
val firstN = plan2.text.toString().toDouble()
val secondN = fact2.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView2.text = it }
} else {
resultTextView2.text = ""
}
}
plan2.addTextChangedListener {
if (!TextUtils.isEmpty(plan2.text) && !TextUtils.isEmpty(fact2.text)) {
val firstN = plan2.text.toString().toDouble()
val secondN = fact2.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView2.text = it }
} else {
resultTextView2.text = ""
}
}
val plan3 = view.findViewById<EditText>(R.id.plan3)
val fact3 = view.findViewById<EditText>(R.id.fact3)
val resultTextView3 = view.findViewById<TextView>(R.id.percent_of_comp3)
fact3.addTextChangedListener {
if (!TextUtils.isEmpty(plan3.text) && !TextUtils.isEmpty(fact3.text)) {
val firstN = plan3.text.toString().toDouble()
val secondN = fact3.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView3.text = it }
} else {
resultTextView3.text = ""
}
}
plan3.addTextChangedListener {
if (!TextUtils.isEmpty(plan3.text) && !TextUtils.isEmpty(fact3.text)) {
val firstN = plan3.text.toString().toDouble()
val secondN = fact3.text.toString().toDouble()
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val result = secondN / firstN * 100
"${df.format(result)}%".also { resultTextView3.text = it }
} else {
resultTextView3.text = ""
}
}
}
}
Ответы (1 шт):
Автор решения: ЮрийСПб
→ Ссылка
Скорее всего проблема в том как вы нав. контроллер ищете. Попробуйте заменить
Navigation.findNavController(it!!).navigate(R.id.action_editBoardFragment_to_boardFragment)
на
Navigation.findNavController(view!!).navigate(R.id.action_editBoardFragment_to_boardFragment)