gpt4 book ai didi

android - 如何在 Kotlin 的抽屉导航 Activity 中添加 OnFragmentInteractionListener?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:02 25 4
gpt4 key购买 nike

我正在尝试制作一个使用 Navigation Drawer Activity 的应用程序但是我需要使用 Fragments 当我添加 fragment 时,并试图在 fragment 之间切换它崩溃了我不知道为什么它会崩溃事务它抛出错误:

 Process: com.a3.aakap.ftrial, PID: 6958
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a3.aakap.ftrial/com.a3.aakap.ftrial.MainActivity}: java.lang.RuntimeException: com.a3.aakap.ftrial.MainActivity@9483ac4 must implement OnFragmentInteractionListener

我无法修复它,因为我是新手这是我的 MainAcivity.kt 文件:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)

fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show()
}

val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()

nav_view.setNavigationItemSelectedListener(this)

title = "Fragment One"
val fragment = FirstFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.first_activity, fragment, "FragmentOne") //create first framelayout with id fram in the activity where fragments will be displayed
fragmentTransaction.commit()
}

override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.first_activity -> {
title = "Fragment One"
val fragment = FirstFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.first_activity, fragment, "FragmentOne") //create first framelayout with id fram in the activity where fragments will be displayed
fragmentTransaction.commit() // Handle the camera action
}
R.id.second_activity -> {
title = "Fragment Second"
val fragment = FirstFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.second_activity, fragment, "FragmentOne") //create first framelayout with id fram in the activity where fragments will be displayed
fragmentTransaction.commit()
}

R.id.nav_share -> {

}
R.id.nav_exit -> {
System.exit(0)
}
}

drawer_layout.closeDrawer(GravityCompat.START)
return true
}

请帮助我,我已经尝试修复它但我不能请帮助我

这是我的第一个 fragment 代码:

package com.a3.aakap.ftrial

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/** * 一个简单的 [Fragment] 子类。 * 包含此 fragment 的 Activity 必须实现 * [FirstFragment.OnFragmentInteractionListener] 接口(interface) * 处理交互事件。 * 使用 [FirstFragment.newInstance] 工厂方法来 * 创建这个 fragment 的一个实例。 */FirstFragment 类: fragment (){

// TODO: Rename and change types of parameters
private var mParam1: String? = null
private var mParam2: String? = null

private var mListener: OnFragmentInteractionListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
mParam1 = arguments.getString(ARG_PARAM1)
mParam2 = arguments.getString(ARG_PARAM2)
}
}

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_first, container, false)
}

// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(uri: Uri) {
if (mListener != null) {
mListener!!.onFragmentInteraction(uri)
}
}

override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
mListener = context
} else {
throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
}
}

override fun onDetach() {
super.onDetach()
mListener = null
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
*/
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}

companion object {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private val ARG_PARAM1 = "param1"
private val ARG_PARAM2 = "param2"

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FirstFragment.
*/
// TODO: Rename and change types and number of parameters
fun newInstance(param1: String, param2: String): FirstFragment {
val fragment = FirstFragment()
val args = Bundle()
args.putString(ARG_PARAM1, param1)
args.putString(ARG_PARAM2, param2)
fragment.arguments = args
return fragment
}
}

}//需要空公共(public)con

这是我的 Fragement xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.a3.aakap.ftrial.FirstFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>

最佳答案

显然,您正在尝试通过 OnFragmentInteractionListener 接口(interface)实现 fragment 和 Activity 之间的通信机制,因此请使用

class MainActivity : AppCompatActivity(),
FirstFragment.OnFragmentInteractionListener,
NavigationView.OnNavigationItemSelectedListener {
// .. other code

override fun onFragmentInteraction(uri: Uri) {
// TODO Implement
}


}

和覆盖方法

关于android - 如何在 Kotlin 的抽屉导航 Activity 中添加 OnFragmentInteractionListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48305360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com