English 中文(简体)
重新启动和重新启动“观点”。
原标题:When the activity was recreated and reinitiate ViewPager2, the requireContext() and requireView() is null

当信安装置的定位器发生变化时,活动就会重新进行。 当时,在从这些活动中获取碎片功能时,要求“Context()”和要求“View()”即为无效,而“Model”则不是开始。

这是我的法典。

活动

    var pager改编: NewPager改编? = null
    fun initView(){
        val vpContent = findViewById<ViewPager2>(R.id.vp_content)
        val fragmentList = ArrayList<家长筹资>()
        fragmentList.add(FristFragment())
        fragmentList.add(SecondFragment())
        fragmentList.add(ThirdFragment())
        pager改编 = NewPager改编(fragmentList, supportFragmentManager, lifecycle)
        vpContent.adapter = pager改编

        val btnRefresh = findViewById<Button>(R.id.btn_scm_refresh)
        btnRefresh.setOnClickListener {
            (pager改编)!!.allRefreshPage()
        }
    }

改编

    override fun getItemCount(): Int {
        return fragmentList.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragmentList[position]
    }

    fun allRefreshPage(context: Context){
        for(i in 0 until fragmentList.size){
            fragmentList[i].refreshData()
        }
    }

家长筹资

abstract class 家长筹资 : Fragment() {
    abstract fun refreshData()
}

童工

    private lateinit var viewModel: NewViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = ViewModelProvider(this, ViewModelFactory())[NewViewModel::class.java]
    }


    override fun refreshData() {
        if(context == null || view == null) {
            Log.w(TAG, "refreshData: have not context or view")
            return
        }
        viewModel.initAppList(requireContext(), requireView())
    }

  1. 为什么发生这种情况?

  2. 我怎么能确保失败?

  3. 是否有其他办法将它作为 fragment在活动中的职能?

问题回答

<>/>?

页: 1 清单只是为了在必要时确定数据而节省了碎块,因此不应使用碎块变数来更新国际交易日志。 虽然启动了支离破碎,但它没有附属于该项活动(用户的斜 t向该职位上滚滚动)。 如无附件<代码>Fragment <>/code>,getContextgetActective 始终无效。

Your Fra

Note that this can be called while the fragment s activity is still in the process of being created. As such, you can not rely on things like the activity s content view hierarchy being initialized at this point

http://stackoverflow.com/questions/50257505/fragment-not-attadated-to-a-context/50257662#50257662"

2/ How can 我请我确保不公开?

正如我说过的那样,没有利用改编者中的“框架”清单更新你的“框架”数据,从而可能引发“倡议”的最新情况。 浏览器可以确保任何要求更新《国际交易日志》和要求了解情况的要求,在《意见书》之后,在“意见编码”和<编码>之间有非核背景。 DestroyView

3/ 是否有其他办法将其作为活动碎片的功能?

a/ 您应使用<条码>代号令/代码>代表创建“观点”以避免在错误立场上形成“观点”;<条码> 字典> 字典<<<> >将确保你确信> 活动案例附有观点模型,活动与碎裂都给你同样的观点模型,这样你就可以在<条码>>代谢/代码>和<条码>之间分享数据。

private val viewModel by activityViewModels<TextClassifyViewModel>()

b/ 在<代码>LiveData内存储你的数据,然后从上登录。 这样,每当碎块重新编号时,LiveData将再次向数据火上报,以更新观点。 无需每次打电话<代码>。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签