gpt4 book ai didi

android - 在 Fragment Kotlin 中调用 DialogFragment

转载 作者:行者123 更新时间:2023-12-02 13:08:37 26 4
gpt4 key购买 nike

我正在尝试将我的 dialogfragment 调用到我的 loginfragment 中并显示一个警报对话框,该 show 方法说:

None of the following functions can be called with the arguments supplied. show(FragmentManager!, String!) defined in org.greenstand.android.TreeTracker.fragments.CustomDialogFragment show(FragmentTransaction!, String!) defined in org.greenstand.android.TreeTracker.fragments.CustomDialogFragment


val newFragment = CustomDialogFragment.newInstance("pass content here")

val fm = fragmentManager
newFragment.show(fm, "look")

这是我的 CustomDialogFragment 代码:
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenstand.android.TreeTracker.R
import org.greenstand.android.TreeTracker.application.Permissions
import org.greenstand.android.TreeTracker.utilities.*
import org.greenstand.android.TreeTracker.viewmodels.LoginViewModel
import org.koin.android.viewmodel.ext.android.viewModel
import timber.log.Timber
class CustomDialogFragment : DialogFragment()
{
private var content: String? = null

override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
content = arguments!!.getString("content")

val style = DialogFragment.STYLE_NO_FRAME
val theme = R.style.DialogTheme
setStyle(style, theme)
}


override fun onAttach(context: Context) {
super.onAttach(context)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle): View
{
val view = inflater!!.inflate(R.layout.layout_dialog, container, false)

val btnAccept = view.findViewById<View>(R.id.buttonAccept) as Button

val textViewContent = view.findViewById<View>(R.id.textViewContent) as TextView
textViewContent.text = content;

btnAccept.setOnClickListener{
dismiss();
}

return view;
}

companion object
{
fun newInstance(content: String) : CustomDialogFragment
{
val f = CustomDialogFragment()

val args = Bundle()
args.putString("content", content)
f.arguments = args

return f
}
}

}

有人可以指出我需要传递给 show 方法的确切内容吗?任何帮助表示赞赏,谢谢:)

最佳答案

[更新]
问题是该方法可以接收到 FragmentManager但它必须不为空,看看错误和不可为空的 Kotlin 符号FragmentManager!!所以你可以做

fm?.let {newFragment.show(fm, "your tag")}
//fm here can also be it
请参阅 Noushad Hasan 答案中的评论
您正在通过 FragmentManager对于该方法,它需要 FragmentTransactionString作为标签:
val transaction = supportFragmentManager.beginTransaction()
newFragment.show(transaction, "SOME_TAG")
几个建议:
你可以让你 DialogFragment使用 Kotlin 标准函数获得更多 kotliny
companion object {

private const val KEY = "param1"

@JvmStatic
fun newInstance(param1: String) =
ExampleDialogFragment().apply {
arguments = bundleOf(KEY to param1)
}
}
您可以通过创建 Fragment 来获得一个很好的示例。使用 Android Studio 向导并检查工厂方法选项。
此外,由于标签将用于您的 DialogFragment您可以将其设为公共(public)常量:
 companion object {
const val TAG = "TAG"
}
也许您想仔细检查对话框 fragment 是否已经存在并将其删除,以实际作为新对话框工作
        val transaction = supportFragmentManager.beginTransaction()
val previous = supportFragmentManager.findFragmentByTag(ExampleDialogFragment.TAG)
if (previous != null) {
transaction.remove(previous)
}
transaction.addToBackStack(null)

val dialogFragment = ExampleDialogFragment.newInstance("parameter")
dialogFragment.show(transaction, ExampleDialogFragment.TAG)

关于android - 在 Fragment Kotlin 中调用 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56316882/

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