android-fragments 在Kotlin片段中调用DialogFragment

ccrfmcuu  于 2022-11-13  发布在  Android
关注(0)|答案(4)|浏览(257)

我尝试将我的dialogfragment调用到我的loginfragment中,并显示一个警告对话框,show方法显示:
无法使用所提供的参数调用以下任何函数。show(FragmentManager!,String!)定义于org.greenstand.android.TreeTracker.fragments中。CustomDialogFragment show(FragmentTransaction!,String!)定义于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方法中的是什么?任何帮助都很感激,谢谢:)

soat7uwm

soat7uwm1#

[UPDATE]问题是该方法可以接收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")

几点建议:
通过使用Kotlin标准函数,您可以使您的DialogFragment更加 kotliny

companion object {

        private const val KEY = "param1"

        @JvmStatic
        fun newInstance(param1: String) =
            ExampleDialogFragment().apply {
                arguments = bundleOf(KEY to param1)
            }
    }

您可以使用Android Studio向导创建一个Fragment并选中工厂方法选项,从而获得一个很好的示例。
此外,由于标记将用于DialogFragment,因此可以将其设为公共常量:

companion object {
        const val TAG = "TAG"
 }

也许你想再次检查对话框片段是否已经存在,并删除它,以实际上作为一个新的对话框工作

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)
oyxsuwqo

oyxsuwqo2#

确保您的Activity是https://developer.android.com/reference/androidx/fragment/app/FragmentActivity.html的子Activity,例如,您可以使用https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity作为父Activity。
然后,使用getSupportFragment()方法或仅使用val fm = supportFragmentManager来获取fragmentManager

vjhs03f7

vjhs03f73#

我在customDialog()类中没有看到DialogFragment导入。实际上,在line no: 9上有一个Fragment导入。你确定你是从正确的DialogFragment扩展的吗?

t40tm48m

t40tm48m4#

试试这个

val newFragment = CustomDialogFragment()
newFragment.show(childFragmentMangar, "look")`

相关问题