如何正确返回fragment_title?使用databinding ActivityMain错误
我知道这个问题已经回答了100次,但我的代码根本不起作用,我无法通过阅读其他人的帖子来弄清楚它是什么问题。
我想制作一个应用程序,当您打开它时,您会提交搜索,您将要搜索的内容提交,然后单击Enter,然后获得搜索的内容。
因此,我正在尝试为此使用数据指标。在活动xml文件中,我用布局标签包装了所有内容:
<?xml version="1.0" encoding="utf-8"?>
&lt; layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/TitleFragment"
/>
&lt;/layout
,这是我的mainactivty:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
}
和我的title fragment:
class TitleFragment : Fragment() {
//Inflating and Returning the View with DataBindingUtil
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_title, container, false)
}
}
我只想能够通过fragment_title.xml通过显示的fragment_title.xml来打开该应用程序activity_main,因为我想使用数据指标来打开我的视图。 我一直在遇到错误“未解决的参考:activityMainBinding”,但在构建中。 databinding = true
ViewBinding = true,
但没有任何可行。 我该如何解决此错误,甚至是否正确返回片段?
I know that this question has been answered 100 times but my code just doesn't work and I can't figure out what is wrong with it by reading posts made by others.
I want to make an app where when you open it u get a search filed, you tipe what u want to search and then click enter and you get what you searched for.
So, I'm trying to use DataBinding for that. In the activity xml file i wrapped everything with the layout tag :
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/TitleFragment"
/>
</layout
And here is my MainActivty:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
}
and my TitleFragment:
class TitleFragment : Fragment() {
//Inflating and Returning the View with DataBindingUtil
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_title, container, false)
}
}
I just want to be able to open the app with the fragment_title.xml showing via the activity_main as i want to use databinding for opening my views.
I keep getting the error "Unresolved reference: ActivityMainBinding" but in the build.gradle i included
databinding=true
viewbinding=true
but nothing works.
How do i fix this error and did i even return the fragment correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标题侵蚀
主动脉
TitleFragment
MainActivity