Don
Don

Reputation: 526

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.

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. 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. I've also tried someView.findViewById() which doesn't work. I'm stumped. Any ideas?

Logcat shows a null pointer, but nothing more:

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 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>

Upvotes: 1

Views: 1884

Answers (1)

curioustechizen
curioustechizen

Reputation: 10672

Where is the addNewView() method being called? From my understanding, if you do a findViewById() in the constructor of a custom view, you could get a 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.

Upvotes: 3

Related Questions