自定义ListView找不到按钮

Custom ListView can't find button

我正在使用可扩展列表视图, 一切正常, 但是由于我添加了一个按钮,它无法通过 id 找到视图。 MainActivity.java:

public class MainActivity extends Activity {

    Button btn;
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // get the listview
        btn=(Button)findViewById(R.id.button); //Cannot Resolve symbol "button"
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

    }
}

可能是什么错误:? activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lvexpandable.MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="450dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="230dp"
            android:text="Button"
            tools:layout_editor_absoluteX="257dp"
            tools:layout_editor_absoluteY="360dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Expandable ListView 工作正常,但 Button 即使与 EX LV 处于相同 xml 也无法识别。

您是否尝试过清理并重建您的项目?这会重新生成所有资源 (R.java),因此您可以在 类 中使用它们。 (构建 -> 清理项目和构建 - 重建项目)