findViewById()找不到自定义视图,但它存在于布局中

时间:2017-12-09 19:45:05

标签: java android

不知怎的,我的acitivity_main.xml有视图BottomSelectElement(自定义视图),但我在活动中找不到它,但我可以找到其他任何内容。

activity_main.xml(删除不必要的部分)

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

我的自定义视图类,在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;
        }
    }

最后我的MainActivity.java没有找到 customBottomSelect ,但它找到了其他任何内容。

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);
    }
}

我多次重启Android Studio,重建并清理它。它似乎没有找到这个id。

1 个答案:

答案 0 :(得分:6)

因为在构造函数中,您有super(context)而不是super(context, attrs)

有道理,如果你没有传递属性,例如id,那么视图将没有id,因此无法使用该id找到。 :-)

回答ref:findViewById() returns null for custom component in layout XML, not for other components