在Fragment中,findViewById()方法是Activity的方法,在Fragment中不能直接使用。要在Fragment中获取View,可以使用onCreateView()方法中的参数View对象来获取。修改代码如下:

class NewsContentFragment : Fragment() { private lateinit var linear1: LinearLayout

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_news_content, container, false)
    linear1 = view.findViewById(R.id.linear1)
    return view
}

}

class NewsContentFragment Fragment val linear1 LinearLayout =findViewByIdRidlinear1中findViewById报错

原文地址: http://www.cveoy.top/t/topic/ieC5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录