[英]findViewById() is returning null for custom view - Android
Let me start by saying this: I've already looked through all of the other threads on this issue and combed Google for an answer, but nothing has helped me. 首先让我说一下:我已经查看过这个问题上的所有其他主题并梳理谷歌的答案,但没有任何帮助我。
I have a layout that contains a custom view that is added at runtime. 我有一个布局,其中包含在运行时添加的自定义视图。 My custom view has the
super(context, attrs)
constructor. 我的自定义视图具有
super(context, attrs)
构造函数。
Custom view: 自定义视图:
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize some variables...
}
I add views to my activity with the following method: 我使用以下方法为我的活动添加了视图:
public void addNewView (boolean isEmpty) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < amount; i++) {
if (isEmpty) {
View v = (ViewGroup) inflater.inflate(R.layout.my_custom_layout, layoutContainer, true);
viewTracker++;
}
}
}
If I use findViewById for my custom view in the addNewView()
method above then all is well. 如果我在上面的
addNewView()
方法中使用findViewById作为我的自定义视图,那么一切都很好。 However, if I try to use findViewById anywhere else in the activity after the view has been inflated (and obviously after setContentView
) I get a null pointer. 但是,如果我在视图充气后尝试在活动中的任何其他地方使用findViewById(显然在
setContentView
之后),我会得到一个空指针。 I've also tried someView.findViewById()
which doesn't work. 我也尝试过
someView.findViewById()
,它不起作用。 I'm stumped. 我很难过。 Any ideas?
有任何想法吗?
Logcat shows a null pointer, but nothing more: Logcat显示一个空指针,但仅此而已:
02-16 06:56:10.681: E/AndroidRuntime(11360): FATAL EXCEPTION: main
02-16 06:56:10.681: E/AndroidRuntime(11360): java.lang.NullPointerException
02-16 06:56:10.681: E/AndroidRuntime(11360): at com.nutrifit.Fragments.WorkoutPlannerPopup.resetOtherView(WorkoutPlannerPopup.java:404)
02-16 06:56:10.681: E/AndroidRuntime(11360): at com.nutrifit.customviews.CustomLinearLayout.onTouchEvent(CustomLinearLayout.java:43)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.View.dispatchTouchEvent(View.java:7239)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
Here's my custom view in XML: 这是我在XML中的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<com.nutrifit.customviews.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exercise_container_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/wpp_background_border" >
<ImageView android:id="@+id/delete_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/wpp_background_border" />
...more android child views
</com.nutrifit.customviews.CustomLinearLayout>
Where is the addNewView()
method being called? 调用
addNewView()
方法在哪里? From my understanding, if you do a findViewById()
in the constructor of a custom view, you could get a null
. 根据我的理解,如果在自定义视图的构造函数中执行
findViewById()
,则可以获得null
。 I haven't found an authoritative reference that states when it is safe to call findViewById()
in a custom view, but I normally do it in onFinishInflate()
or later. 我没有找到一个权威的参考,说明何时在自定义视图中调用
findViewById()
是安全的,但我通常在onFinishInflate()
或更高版本中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.