Android 片段中的参数仍然为null

Android 片段中的参数仍然为null,android,android-fragments,kotlin,android-bundle,Android,Android Fragments,Kotlin,Android Bundle,我试图通过一个包将两个字符串传递给我的另一个片段。问题是,onCreate中的参数仍然为空。这是我的密码: class VenueDetailFragment : Fragment() { private lateinit var venueId: String private lateinit var venueName: String companion object { fun newInstance(venue: Venue): VenueDetailFragment {

我试图通过一个包将两个字符串传递给我的另一个片段。问题是,onCreate中的参数仍然为空。这是我的密码:

class VenueDetailFragment : Fragment() {

private lateinit var venueId: String
private lateinit var venueName: String

companion object {
    fun newInstance(venue: Venue): VenueDetailFragment {
        val frag = VenueDetailFragment()
        val bundle = Bundle()
        bundle.putString(Extra.VENUE_ID_KEY, venue.id)
        bundle.putString(Extra.VENUE_NAME, venue.name)
        frag.arguments = bundle
        return VenueDetailFragment()
    }
}

private lateinit var viewModel: VenueDetailViewModel

override fun onAttach(context: Context) {
    super.onAttach(context)
    viewModel = ViewModelProviders.of(this).get(VenueDetailViewModel::class.java)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        venueId = it.getString(Extra.VENUE_ID_KEY, "0")
        venueName = it.getString(Extra.VENUE_NAME, getString(R.string.venue_detail_title_unknown))
    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
    inflater.inflate(R.layout.venue_detail_fragment, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    viewModel.getVenueDetails(venueId).observe(this, Observer { result ->
        result?.let { safeVenue ->
            venueDetailTitle.text =
                if (safeVenue.name.isNotEmpty()) safeVenue.name else venueName

            venueDetailDescription.text =
                if (safeVenue.description.isNotEmpty()) safeVenue.description
                else getString(R.string.venue_detail_description_unknown)

            venueDetailAddress.text = safeVenue.location.formattedAddress.joinToString(",\n")

            venueDetailRatingBar.rating = (safeVenue.rating / 5).toFloat()

            setupContactView(safeVenue.contact)
        }
    })
}

private fun setupContactView(contact: Contact) {
    venueDetailTwitterBtn.visibility = getVisibility(contact.twitter)
    venueDetailInstagramBtn.visibility = getVisibility(contact.instagram)
    venueDetailFacebookBtn.visibility = getVisibility(contact.facebook)
    venueDetailCallBtn.visibility = getVisibility(contact.phone)
}

private fun getVisibility(contactType: String?): Int {
    return if (contactType.isNullOrEmpty()) View.VISIBLE else View.GONE
}
}

我通过以下方式初始化新片段:

private fun navigateToDetailPage(data: List<Venue>) {
    activity?.let { safeActivity ->
        venueResultList.adapter = VenueFinderAdapter(
            safeActivity, data,
            object : VenueFinderAdapter.VenueSelectionListener {
                override fun onVenueSelected(venue: Venue) {
                    safeActivity.supportFragmentManager.beginTransaction().apply {
                        replace(R.id.fragmentContainer, VenueDetailFragment.newInstance(venue))
                        addToBackStack(null)
                        commit()
                    }
                }
            })
    }
}
private fun navigateToDetailPage(数据:列表){
活动?.let{safeActivity->
venueResultList.adapter=VenueFinderAdapter(
安全活动、数据、,
对象:VenueFinderAdapter.VenueSelectionListener{
已选定的地点(地点:地点){
safeActivity.supportFragmentManager.beginTransaction().apply{
替换(R.id.fragmentContainer,VenueDetailFragment.newInstance(场地))
addToBackStack(空)
提交()
}
}
})
}
}
如果我运行此命令,我的两个lateinit变量(字符串)在onViewCreated中仍然为null。我看到另一篇帖子告诉我应该从onCreate中的参数中获取值,正如你所看到的,我也尝试过,但没有成功


有人能告诉我我做错了什么吗?

您正在返回
VenueDetailFragment()
,您应该返回
frag

fun newInstance(venue: Venue): VenueDetailFragment {
    val frag = VenueDetailFragment()
    val bundle = Bundle()
    bundle.putString(Extra.VENUE_ID_KEY, venue.id)
    bundle.putString(Extra.VENUE_NAME, venue.name)
    frag.arguments = bundle
    return frag 
}

您正在返回
VenueDetailFragment()
,您应该返回
frag

fun newInstance(venue: Venue): VenueDetailFragment {
    val frag = VenueDetailFragment()
    val bundle = Bundle()
    bundle.putString(Extra.VENUE_ID_KEY, venue.id)
    bundle.putString(Extra.VENUE_NAME, venue.name)
    frag.arguments = bundle
    return frag 
}

在newInstance方法中返回frag而不是VenueDetailFragment()

fun newInstance(venue: Venue): VenueDetailFragment {
    val frag = VenueDetailFragment()
    val bundle = Bundle()
    bundle.putString(Extra.VENUE_ID_KEY, venue.id)
    bundle.putString(Extra.VENUE_NAME, venue.name)
    frag.arguments = bundle
    return frag
}

在newInstance方法中返回frag而不是VenueDetailFragment()

fun newInstance(venue: Venue): VenueDetailFragment {
    val frag = VenueDetailFragment()
    val bundle = Bundle()
    bundle.putString(Extra.VENUE_ID_KEY, venue.id)
    bundle.putString(Extra.VENUE_NAME, venue.name)
    frag.arguments = bundle
    return frag
}

哇,我能把我的30分钟拿回来吗?谢谢道沃,我能把我的30分钟拿回来吗?谢谢D