У меня проблема с передачей данных из фрагмента в другой фрагмент.
Ошибка:
Причина: java.lang.IllegalStateException: Fragment FragmentBayar {5cd96b2} не присоединен к контексту.
на android.support.v4.app.Fragment.requireContext (Fragment.java:614)
на android.support.v4.app.Fragment.getResources (Fragment.java:678)
на android.support.v4.app.Fragment.getString (Fragment.java:700)
в com.kensai.appkasir.fragment.FragmentBayar $ Companion.newInstance (FragmentBayar.kt: 45)
в com.kensai.appkasir.FragmentActivity.onCreate (FragmentActivity.kt: 55)
на android.app.Activity.performCreate (Activity.java:6303)
на android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
atroid.app.ActivityThread.performLaunchActivity (ActivityThread.java:2376)
Вот мой фрагмент
class FragmentBayar : Fragment(){
// order information
private lateinit var order: Orders
// data of menu before send
private var editList = ArrayList<EditQuantity>()
private lateinit var dataEditAdapter: EditQuantityAdapter
// data of menu after send
private var progressList = ArrayList<ProgressAntar>()
private lateinit var dataProgressAdapter: ProgressAntarAdapter
companion object {
fun newInstance(order: Orders): FragmentBayar {
val fragment = FragmentBayar()
val bundle = Bundle()
bundle.putParcelable(fragment.getString(R.string.key_pass_order), order)
fragment.arguments = bundle
return fragment
}
}
// update array list of edit menu from clicked menu fragment
fun updateOrderEditList(menu: Menu){
val editMenu = EditQuantity(menu,1,"")
editList.add(0,editMenu)
rec_edit_quantity.adapter.notifyDataSetChanged()
}
// read argumen as order object
private fun readBundle(bundle: Bundle?) {
if (bundle != null) {
order = bundle.getParcelable(getString(R.string.key_pass_order))
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_bayar, container, false)
// get order
readBundle(arguments)
// change name order in header of view
view.order_name.text = order.order
// initial recyclerview edit item
view.rec_edit_quantity.layoutManager = LinearLayoutManager(activity)
view.rec_edit_quantity.isNestedScrollingEnabled = false
view.rec_edit_quantity.hasFixedSize()
view.rec_edit_quantity.adapter = EditQuantityAdapter(editList){ editPosition: Int -> editItemClicked(editPosition) }
// initial recyclerview edit item data
dataEditAdapter = EditQuantityAdapter(editList){ editPosition: Int -> editItemClicked(editPosition) }
view.rec_edit_quantity.adapter = dataEditAdapter
// initial recyclerview progress item
view.rec_progress_antar.layoutManager = LinearLayoutManager(activity)
view.rec_progress_antar.isNestedScrollingEnabled = false
view.rec_progress_antar.hasFixedSize()
view.rec_progress_antar.adapter = ProgressAntarAdapter(progressList)
// initial recyclerview progress item data
dataProgressAdapter = ProgressAntarAdapter(progressList)
view.rec_progress_antar.adapter = dataProgressAdapter
// setup pesan button
btn_pesan.setOnClickListener {
sendOrder()
}
// get detail service from server
getOrderDetailService()
return view
}
// call to get order detail from web service
private fun getOrderDetailService() {
//temp
progressList = getOrderData()
dataProgressAdapter.updateData(progressList)
}
// send order list to server
private fun sendOrder(){
val apiService : Service = Client.getClient()!!.create(Service::class.java)
apiService.sendOrder(editList).enqueue(object : Callback<List<ProgressAntar>> {
override fun onResponse(call: Call<List<ProgressAntar>>?, response: Response<List<ProgressAntar>>?) {
if (response != null && response.isSuccessful) {
val list = response.body()
if (list == null || list.isEmpty()) {
Toast.makeText(activity, "Tidak ada daftar pembayaran", Toast.LENGTH_LONG).show()
} else{
// empty edit list
editList = ArrayList()
dataEditAdapter.updateData(editList)
// refresh progress list
progressList = ArrayList(list)
dataProgressAdapter.updateData(progressList)
}
} else{
Toast.makeText(activity, "Tidak ada daftar pembayaran", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<List<ProgressAntar>>?, t: Throwable?) {
Log.i("onFailure", t.toString())
Toast.makeText(context, "Gagal", Toast.LENGTH_LONG).show()
}
})
}
// delete menu item before send
private fun editItemClicked(menuItem : Int){
editList.removeAt(menuItem)
rec_edit_quantity.adapter.notifyDataSetChanged()
}
// temp function
private fun getOrderData(): ArrayList<ProgressAntar>{
val list = ArrayList<ProgressAntar>()
list.add(ProgressAntar("1","Bakso",15000, 4,0,false))
list.add(ProgressAntar("2","Teh Panas", 15000, 3,0, false))
list.add(ProgressAntar("3","Mie Aceh", 15000, 5,5, true))
list.add(ProgressAntar("4","Teh Dingin", 15000, 3,3, true))
return list
}
override fun onResume() {
super.onResume()
getOrderDetailService()
}
}
Это класс деятельности
class FragmentActivity : AppCompatActivity(), FragmentKategori.OnItemSelectedListener,
FragmentMenu.OnItemSelectedListener {
// current order information
private lateinit var order: Orders
// action for item clicked on category fragment
override fun onCategoryItemSelected(category: Category) {
val fragment = supportFragmentManager.findFragmentById(R.id.placeholder2) as FragmentMenu
fragment.updateMenuList(category)
}
// action for item clicked on menu fragment
override fun onMenuItemSelected(menu: Menu) {
val fragment = supportFragmentManager.findFragmentById(R.id.placeholder3) as FragmentBayar
fragment.updateOrderEditList(menu)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_fragment)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
supportActionBar?.setDisplayHomeAsUpEnabled(true)
// get bundle from previous activity
val bundle = this.intent.extras
// get order
if(bundle.containsKey(getString(R.string.passOrder))){
order = bundle.getParcelable(getString(R.string.passOrder))
}
else {
Toast.makeText(this, "Something wrong", Toast.LENGTH_SHORT).show()
finish()
}
val fragment1 = FragmentKategori()
val fragment2 = FragmentMenu()
val fragment3 = FragmentBayar.newInstance(order)
supportFragmentManager.beginTransaction()
.replace(R.id.placeholder1,fragment1)
.replace(R.id.placeholder2,fragment2)
.replace(R.id.placeholder3,fragment3)
.commit()
}
override fun onCreateOptionsMenu(menu: android.view.Menu?): Boolean {
menuInflater.inflate(R.menu.search,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val id = item?.itemId
if (id == R.id.search){
swipeView()
}else if (id == android.R.id.home){
finish()
}
return super.onOptionsItemSelected(item)
}
// change view to search
private fun swipeView(){
// show search view
if (rec_menu.visibility == View.VISIBLE){
rec_menu.visibility = View.GONE
lay_search.visibility = View.VISIBLE
}
// show menu view
else{
rec_menu.visibility = View.VISIBLE
lay_search.visibility = View.GONE
}
}
}