我正在尝试调用我的对话片段到我的日志基础设施中,并显示一个警告对话框,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方法中吗?感谢您的帮助,谢谢:)
发布于 2019-05-27 05:16:10
更新问题是该方法可以接收FragmentManager
,但它必须不为空,请查看错误和不可为空的Kotlin符号
FragmentManager!!
所以你可以这样做
fm?.let {newFragment.show(fm, "your tag")}
//fm here can also be it
请参阅Noushad答案中的评论
您正在将FragmentManager
传递给该方法,它需要FragmentTransaction
和String
作为标记:
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)
发布于 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
发布于 2019-05-27 05:04:16
我在您的customDialog()
类中看不到DialogFragment
导入。实际上,在line no: 9
上有一个片段导入。您确定是从正确的DialogFragment
扩展的吗
https://stackoverflow.com/questions/56316882
复制相似问题