首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在片段Kotlin中调用DialogFragment

在片段Kotlin中调用DialogFragment
EN

Stack Overflow用户
提问于 2019-05-27 03:51:31
回答 3查看 9.8K关注 0票数 1

我正在尝试调用我的对话片段到我的日志基础设施中,并显示一个警告对话框,show方法说:

无法使用提供的参数调用以下任何函数。show(FragmentManager!,String!)在org.greenstand.android.TreeTracker.fragments.CustomDialogFragment show(FragmentTransaction!,String!)中定义在org.greenstand.android.TreeTracker.fragments.CustomDialogFragment中定义

代码语言:javascript
复制
val newFragment = CustomDialogFragment.newInstance("pass content here")

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

下面是我的CustomDialogFragment代码:

代码语言:javascript
复制
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方法中吗?感谢您的帮助,谢谢:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-05-27 05:16:10

更新问题是该方法可以接收FragmentManager,但它必须不为空,请查看错误和不可为空的Kotlin符号

FragmentManager!!

所以你可以这样做

代码语言:javascript
复制
fm?.let {newFragment.show(fm, "your tag")}
//fm here can also be it

请参阅Noushad答案中的评论

您正在将FragmentManager传递给该方法,它需要FragmentTransactionString作为标记:

代码语言:javascript
复制
val transaction = supportFragmentManager.beginTransaction()
newFragment.show(transaction, "SOME_TAG")

以下是一些建议:

您可以通过使用Kotlin标准函数使您的DialogFragment更具kotliny

代码语言:javascript
复制
companion object {

        private const val KEY = "param1"

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

您可以通过使用Android Studio向导创建一个Fragment并检查工厂方法选项来获得一个很好的示例。

此外,由于标记将用于您的DialogFragment,因此可以将其设置为公共常量:

代码语言:javascript
复制
 companion object {
        const val TAG = "TAG"
 }

您可能需要仔细检查对话框片段是否已经存在,并将其删除,以便实际作为新的对话框工作

代码语言:javascript
复制
        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)
票数 3
EN

Stack Overflow用户

发布于 2019-05-27 04:06:08

确保您的活动是https://developer.android.com/reference/androidx/fragment/app/FragmentActivity.html的子活动,例如,您可以使用https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity作为父活动。

然后使用getSupportFragment()方法或仅使用val fm = supportFragmentManager来获取fragmentManager

票数 1
EN

Stack Overflow用户

发布于 2019-05-27 05:04:16

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56316882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档