Custom ListView can't find button

Viewed 37

I'm using Expandable listView, Everything is working fine, But Since I added a button, It can't findview by 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);

    }
}

What could be the error:? 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 is working fine, But Button is not being recognized even being in same xml as EX LV.

1 Answers

Have you tried to clean and rebuild your project? This regenerates all the resources (R.java) so you can use them in your classes. (Build -> Clean Project and Build - Rebuild Project)

Related