Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TabAdapter中的片段拒绝更新TextView_Android_Kotlin_Android Tablayout - Fatal编程技术网

Android TabAdapter中的片段拒绝更新TextView

Android TabAdapter中的片段拒绝更新TextView,android,kotlin,android-tablayout,Android,Kotlin,Android Tablayout,我有一个超级简单的片段,它存在于选项卡适配器中。由于某种原因,我无法更新其中一个文本视图。这是它的样子。我想更新显示“hello there home fragment”(大一点的)的textView 以下是我的主要活动: class MainActivity: AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceSt

我有一个超级简单的
片段
,它存在于
选项卡适配器
中。由于某种原因,我无法更新其中一个
文本视图。这是它的样子。我想更新显示“hello there home fragment”(大一点的)的
textView

以下是我的主要活动:

class MainActivity: AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        setSupportActionBar(toolbar)
        getSupportActionBar()?.hide();


        view_pager.adapter = TabsAdapter(supportFragmentManager)

        tab_layout.setupWithViewPager(view_pager)

    }
}
这是我的标签:

class TabsAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {

    private val fragmentTitles = arrayOf("Home", "Music", "Map")

    override fun getItem(position: Int): Fragment {
        when (position) {
            0 ->
                return HomeFragment()
            1 ->
                return MusicFragment()
            2 ->
                return MapFragment()
            else ->
                return HomeFragment()
        }
    }
    // Return the number of views/fragments available.
    override fun getCount(): Int = 3 // in our case 3 fragments
    // Return title based on position
    override fun getPageTitle(position: Int): CharSequence {
        return fragmentTitles[position]
    }
}
这是我的家庭片段()

但是

主屏幕不会改变,即使它说textView.text值应该不同。我已经仔细检查了textView项是否是我的layouts包中的正确项。我还尝试在onViewCreated生命周期挂钩中设置文本,但这似乎也不起作用


这真的不应该这么难。我已经在stackoverflow和文档中查找了好几天,我能告诉你的是,这是你应该做的事情。如果有任何帮助,我们将不胜感激。

请更改您的方法,以返回
视图,而不是将其再次膨胀

class HomeFragment : Fragment() {

    var weathermainView: TextView?=null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {


        val view = inflater!!.inflate(R.layout.fragment_home, container, false)

        val weathermainView = view.findViewById<TextView>(R.id.textweathermain)



        println("***********************************************")
        println("value of weathermainView in onCreateView BEFORE setting: " + weathermainView?.text)
        println("***********************************************")
        weathermainView.setText("YOLO DAWG")
        println("***********************************************")
        println("value of weathermainView in onCreateView AFTER setting: " + weathermainView?.text)
        println("***********************************************")
        return view //< --- do this
    }
}
class HomeFragment:Fragment(){
var weathermainView:TextView?=null
覆盖创建视图(充气机:布局充气机?,容器:视图组?,
savedInstanceState:捆绑?:查看{
val view=充气机!!.充气(R.layout.fragment\u home,container,false)
val weathermainView=view.findViewById(R.id.textweathermain)
println(“**********************************************************”)
println(“设置前onCreateView中weathermainView的值:“+weathermainView?”text)
println(“**********************************************************”)
weathermainView.setText(“YOLO DAWG”)
println(“**********************************************************”)
println(“设置后onCreateView中weathermainView的值:”+weathermainView?.text)
println(“**********************************************************”)
返回视图/<---执行此操作
}
}

那么,您想在更改选项卡时更改文本吗?是的!我已经弄明白了如何(最终)通过TabsAdapter通过新实例发送数据。但是在我设置好所有这些之后,我发现我甚至不能更新textView的值。我不知道为什么这会这么难。我以为你想在更改标签时更新片段,哈哈
I/System.out: ***********************************************
I/System.out: value of weathermainView in onCreateView BEFORE setting: hello there home fragment
I/System.out: ***********************************************
I/System.out: ***********************************************
I/System.out: value of weathermainView in onCreateView AFTER setting: YOLO DAWG
I/System.out: ***********************************************
class HomeFragment : Fragment() {

    var weathermainView: TextView?=null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {


        val view = inflater!!.inflate(R.layout.fragment_home, container, false)

        val weathermainView = view.findViewById<TextView>(R.id.textweathermain)



        println("***********************************************")
        println("value of weathermainView in onCreateView BEFORE setting: " + weathermainView?.text)
        println("***********************************************")
        weathermainView.setText("YOLO DAWG")
        println("***********************************************")
        println("value of weathermainView in onCreateView AFTER setting: " + weathermainView?.text)
        println("***********************************************")
        return view //< --- do this
    }
}