gpt4 book ai didi

java - findViewById() 找不到自定义 View ,但它存在于布局中

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:05 26 4
gpt4 key购买 nike

不知何故,我的 acitivity_main.xml 有 View BottomSelectElement(自定义 View ),但我在 Activity 中找不到它,但我可以找到其他任何东西。

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>

我的自定义 View 类在 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。

最佳答案

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

有道理,如果您不传入属性,例如 id,那么 View 将没有 id,因此无法使用该 id 找到。 :-)

答案编号:findViewById() returns null for custom component in layout XML, not for other components

关于java - findViewById() 找不到自定义 View ,但它存在于布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com