Java – findViewById() The custom View could not be found, but it exists in the layout

findViewById() The custom View could not be found, but it exists in the layout… here is a solution to the problem.

findViewById() The custom View could not be found, but it exists in the layout

Somehow, my acitivity_main.xml has View BottomSelectElement, but I can’t find it in the activity, but I can find anything else.

activity_main.xml (removing unnecessary parts).

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="it.bachmann.studytimer.ui.MainActivity">

<!-- THIS IS THE VIEW I CANNOT FIND IN THE MainActivity.java (BottomSelectElement) -->
    <it.bachmann.studytimer.ui.elements.BottomSelectElement
        android:id="@+id/customBottomSelect"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="bottom|center" />

<com.github.clans.fab.FloatingActionMenu

</android.support.design.widget.CoordinatorLayout>

My custom View class looks perfectly in activity_main.xml

public class BottomSelectElement extends ConstraintLayout {

private Spinner spinner;

public BottomSelectElement(Context context) {
        super(context);

init();
        }

public BottomSelectElement(Context context, AttributeSet attributeSet) {
            super(context);
            init();
        }

private void init() {
            inflate(getContext(), R.layout.bottom_select_element, this);
            spinner = findViewById(R.id.spinner);
            List<String> categories = Arrays.asList("foo", "bar", "baz");
            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text, categories);
            adapter.setDropDownViewResource(R.layout.spinner_text_checked);
            spinner.setAdapter(adapter);
        }

public Spinner getSpinner() {
            return spinner;
        }
    }

Finally, my MainActivity .java, which didn’t find customBottomSelect, but it found anything else.

public class MainActivity extends AppCompatActivity {

private final String TAG = this.getClass().getSimpleName();

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

@Override
    protected void onStart() {
        super.onStart();
        BottomSelectElement bottomSelectElement = findViewById(R.id.customBottomSelect);
        Log.d(TAG, "onStart: bottomSelectElement " + bottomSelectElement);  this returns null, althought it should exist!
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

I rebooted Android Studio several times, rebuilt and cleaned it up. It just doesn’t seem to find this id.

Solution

Because in the constructor, you have super(context) instead of super(context, attrs).

It makes sense that if you don’t pass in a property, such as id, then View will not have an id, so it can’t be found using that id. 🙂

Answer number: findViewById() returns null for custom component in layout XML, not for other components

Related Problems and Solutions