Android 有没有一种方法可以知道视图是如何为null的,即使它是声明和调用的?
编辑好了,伙计们,我真的试着给fragment_home里面的FAB打电话,在另一个fragment里,我错了,我应该给Homefragment里面打电话的,我刚刚改变了它,现在它工作了,谢谢大家的帮助 给任何犯同样错误的人 -检查您是否声明了视图的ID -检查您是否在该活动类中(activity_main.xml->MainActivity.kt) -正确调用视图。 如果这不能解决问题,你会在下面找到很好的答案,祝你好运 我有一家工厂,声明如下:Android 有没有一种方法可以知道视图是如何为null的,即使它是声明和调用的?,android,kotlin,Android,Kotlin,编辑好了,伙计们,我真的试着给fragment_home里面的FAB打电话,在另一个fragment里,我错了,我应该给Homefragment里面打电话的,我刚刚改变了它,现在它工作了,谢谢大家的帮助 给任何犯同样错误的人 -检查您是否声明了视图的ID -检查您是否在该活动类中(activity_main.xml->MainActivity.kt) -正确调用视图。 如果这不能解决问题,你会在下面找到很好的答案,祝你好运 我有一家工厂,声明如下: <?xml version="
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseFragment"
android:background="@color/darkBlack">
<TextView
android:id="@+id/my_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:text="@string/notes"
android:textColor="@color/white"
android:textSize="@dimen/_20sdp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchIcon="@drawable/ic_search"
android:background="@drawable/background"
app:queryHint="Search..."
app:defaultQueryHint="Search..."
android:theme="@style/ThemeOverlay.search"
android:iconifiedByDefault="false"
app:layout_constraintTop_toBottomOf="@+id/my_notes"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="10dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_view" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/darkBlack"
android:orientation="horizontal"
android:padding="@dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_check_24"
app:tint="@color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_10sdp"
android:src="@drawable/ic_image"
app:tint="@color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_10sdp"
android:src="@drawable/ic_baseline_insert_link_24"
app:tint="@color/white" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/yellow"
android:id="@+id/fabBtnCreateNote"
android:tint="@color/yellow"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_20sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_add"
android:contentDescription="Fab" />
</androidx.constraintlayout.widget.ConstraintLayout>
它指向fabBtnCreateNote.setOnClickListener{}行
我不明白这意味着什么,有人能帮我吗?尝试使用“findViewById”函数,如下面的答案所示:
我已经有一段时间没有这样做了,但我认为@id属性只创建了一个字符串id,您必须通过R访问它,并使用它返回实际视图
编辑:响应您的编辑:
你必须做到:
var fabBtnCreateNote = view.findViewById<FloatingActionButton>(R.id.fabBtnCreateNote)
var fabBtnCreateNote=view.findviewbyd(R.id.fabBtnCreateNote)
尝试使用“findViewById”函数,如以下答案所示:
我已经有一段时间没有这样做了,但我认为@id属性只创建了一个字符串id,您必须通过R访问它,并使用它返回实际视图
编辑:响应您的编辑:
你必须做到:
var fabBtnCreateNote = view.findViewById<FloatingActionButton>(R.id.fabBtnCreateNote)
var fabBtnCreateNote=view.findviewbyd(R.id.fabBtnCreateNote)
请注意,您需要使用view
调用findViewById()
,因为这是包含FAB的父级
既然fabBtnCreateNote
具有对FAB视图对象的有效引用,您可以对其调用方法,例如setOnClickListener()
请注意,您需要使用view
调用findViewById()
,因为这是包含FAB的父级
<> > <代码> FABBTNCREATENOTE/<代码>对Fab视图对象有一个有效的引用,您可以调用它的方法,如<代码> StEngCLIKListNEnter()/< > < P>如果您特别想使用合成材料,可以考虑如下:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.your_fragment, container, false)
}
然后使用
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
yourButton.whatever
}
因为<代码> OnCureTeVIEW >发生在代码> OnVIEW创建之前,所以在OnVIEW中创建,您可以访问您的按钮
如果您特别想使用合成材料,您可以考虑如下:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.your_fragment, container, false)
}
然后使用
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
yourButton.whatever
}
因为
onCreateView
发生在onViewCreated
之前,所以在onViewCreated内部,您可以访问按钮回答
好了,伙计们,我真的试着给fragment_家里的晶圆厂打电话,在另一个片段中,我的错误,我应该给Homefragment里面打电话,我刚刚改变了它,现在它工作了,谢谢大家的帮助
给任何犯同样错误的人
- 检查您是否声明了视图的ID
- 检查您是否在该活动类中(activity_main.xml->MainActivity.kt)
- 正确调用视图
Android setOnClickListener方法-如何工作?回答 好了,伙计们,我真的试着给fragment_家里的晶圆厂打电话,在另一个片段中,我的错误,我应该给Homefragment里面打电话,我刚刚改变了它,现在它工作了,谢谢大家的帮助 给任何犯同样错误的人
- 检查您是否声明了视图的ID
- 检查您是否在该活动类中(activity_main.xml->MainActivity.kt)
- 正确调用视图
Android setOnClickListener方法-它是如何工作的?@a\u local\u没有人能详细说明一下,我做错了什么?@a\u local\u没有人我看到了这个答案,但我不太明白,我无法理解我做错了什么,我能得到一些帮助吗?你在onCreateView中做什么?“有没有办法知道视图是如何为null的,即使它已被声明和调用?”这还不够。您还需要初始化变量。对于视图,这通常通过
view.findviewbyd()完成
。不,你必须仔细看看我在问什么:)我不是在问你在创建视图时在做什么,我是在问你在创建视图时在做什么different@a_local_nobody请你详细说明一下,我做错了什么?@a_local_没有人我见过这个答案,但我不太明白,我我不能理解我做错了什么,我能得到一些帮助吗?你在onCreateView中做什么?“有没有办法知道视图是如何为null的,即使它被声明和调用了?”这还不够。你还需要初始化变量。对于视图,这通常是通过view.findViewById()完成的
。不,你必须仔细看看我在问什么:)我不是在问你在创建视图时在里面做什么,我是在问你在创建视图时在做什么-它们是不同的非常感谢你的回答,我刚刚编辑了这篇文章来说明这一点,我做了,但仍然得到相同的错误。@TomasMota editedI tried,将晶圆厂id替换为“视图“,但我还是明白error@TomasMota如果您仍然得到NPE,请告诉我们是哪一行引起的。现在我们只是猜测问题出在哪里。如果您能准确地告诉我们是哪条线路导致了问题,我们可以更好地帮助您。另外,请务必阅读问题下方的链接,该链接解释了NPE是什么,以便您可以开始学习如何在将来自己解决此类问题。非常感谢,伙计,我刚刚在抛出错误的行上添加了一条评论。非常感谢
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
yourButton.whatever
}