Fragment 中的参数仍然为空

arguments in Fragment is still null

我正在尝试将两个字符串通过一个包传递到我的另一个片段。问题是,参数在 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()
                    }
                }
            })
    }
}

如果我 运行 这个,我的两个 lateinit 变量(字符串)在 onViewCreated 中仍然是 null。我看到另一个 post 告诉我你应该从 onCreate 的参数中获取值,正如你所看到的,我也尝试过,但没有成功。

谁能告诉我我做错了什么?

你正在returnVenueDetailFragment(),你应该returnfrag

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 
}

return 在你的 newInstance 方法中使用片段而不是 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
}