android-fragments Android工作室:设置片段中的文本

jogvjijk  于 2022-11-14  发布在  Android
关注(0)|答案(3)|浏览(133)

我最近在Kotlin启动了一个Android Studio项目,它有三个片段,可以通过底部的导航栏进行导航。
现在,把我当前的问题分解成一个简单的例子,甚至对我不起作用:假设fragment_home.xml中有一个editText对象exercise,我想在HomeFragment.kt中调用并修改它。我检查了我能从Google和Stackoverflow找到的所有建议来源,并在HomeFragment.kt中得到了以下代码(部分由AndroidStudio预编码):

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

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

        view.exercise.setText("This is an exceptionally hardcoded string")

最后一行代表我尝试访问的每个对象。我还尝试了onClickListening,以查找如下按钮:

val btnNewExercise =  view.findViewById<Button>(R.id.btn_new_exercise)
btnNewExercise.setOnClickListener {view

            Toast.makeText(view.context, "New exercise will be generated", Toast.LENGTH_SHORT).show()
            println("Generated a new exercise")
        }

但是当我启动应用程序/点击按钮时什么都没有发生--我似乎无法通过实际视图的对象来访问它们。甚至在我去解决方案的路上遇到了NullPointerExceptions。
如果需要的话,我可以提供片段和布局文件--只是一开始觉得这样可能更容易。
如果有人能告诉我我错在哪里,我将非常感激!提前感谢!

pengsaosao

pengsaosao1#

你放大了两次布局。
删除此内容。您已经在 FragmentHomeBinding.inflate... 调用中使用视图绑定扩大了布局

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

并且替换了

view.exercise.setText("This is an exceptionally hardcoded string")

with(使用绑定

binding.exercise.setText("This is an exceptionally hardcoded string")

那么onCreateView上的最后一行应该是

return binding.root

注意:您应该具有下列类别属性:

private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

因此,它看起来将像这样:

//move your view model as a class property so it will be accessible by other class methods
                 private val homeViewModel =
                          ViewModelProvider(this).get(HomeViewModel::class.java)

 
                 private var _binding: FragmentHomeBinding? = null
                 private val binding get() = _binding!!//transform to immutable
            
                 override fun onCreateView(
                        inflater: LayoutInflater,
                        container: ViewGroup?,
                        savedInstanceState: Bundle?
                    ): View? {

                        
                
                        _binding = FragmentHomeBinding.inflate(inflater, container, false)
                      
                       //use the immutable view binding property
                        binding.exercise.setText("This is an exceptionally hardcoded string")
            
                        return binding.root
                   }

有关详细信息,请阅读view binding

nbewdwxp

nbewdwxp2#

请尝试此解决方案。

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
  val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.exercise.text="This is an exceptionally hardcoded string"

        binding.btnNewExercise.setOnClickListener {
            Toast.makeText(view.context, "New exercise will be generated", Toast.LENGTH_SHORT).show()
            println("Generated a new exercise")

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
okxuctiv

okxuctiv3#

您需要返回在OnCreateView中创建的视图。

相关问题