Findviewbyid returns 为空。但是可以通过获取根视图和遍历子视图来访问视图元素

Findviewbyid returns null. But can access view elements by getting root view and looping through children

我有一个 activity 我想用来配置一个小部件,但我似乎无法使 findViewById() 工作。它 returns null 用于所有元素。

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:name="@+id/testTextView"
    android:text="refresh time (seconds)"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:name="@+id/testEditText"
    android:text="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
<Button
    android:name="@+id/testButton"
    android:text="Button text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

这是我的 activity 的 onCreate 方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_configure_widget);
    Button button = (Button) findViewById(R.id.testButton);
    EditText editText = (EditText) findViewById(R.id.testEditText);
    TextView textView = (TextView ) findViewById(R.id.testTextView);

    button.setText("Button test");
    editText.setText("editText test");
    textView.setText("textView test");
}

每个元素(按钮、editText、textView)都是 null,当我尝试设置文本时它会抛出 NullPointerException。如果我注释掉那些 setText 行,则会显示正确的布局,如 xml.

中所定义

虽然如果我使用 View v = findViewById(android.R.id.content); 获取根元素并从中获取子元素,它会起作用。 像这样:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_configure_widget);
    View v = findViewById(android.R.id.content); //this is a FrameLayout, I don't know if the container is always a framelayout
    ViewGroup firstChild = (ViewGroup)((ViewGroup)v).getChildAt(0); //this gets my linearlayout
    TextView textView = (TextView)(firstChild.getChildAt(0));
    EditText editText = (EditText)(firstChild.getChildAt(1));
    Button button = (Button)(firstChild.getChildAt(2));

    button.setText("Button test");
    editText.setText("editText test");
    textView.setText("textView test");
}

使用此布局显示,每个元素的文本设置为 "Button test" "editText test" 和 "textView test",如预期的那样。

我已经清理并重建了项目。在调试时,我检查过资源名称实际上解析为 ID 号。

谁能解释一下这是怎么回事?为什么我的 findViewById() 不适合我?

您的视图没有任何 ID。使用 android:id 而不是 android:name 来声明它们。

您需要为您希望在代码中访问的那些视图声明属性 android:id。因此,您需要将此属性添加到 xml 文件中的视图。

将您的 layout.xml 文件更改为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/testTextView"
    android:text="refresh time (seconds)"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:id="@+id/testEditText"
    android:text="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />
<Button
    android:id="@+id/testButton"
    android:text="Button text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>