0

I created a activity that contain a listview and a button

<?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">
    <ListView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@android:id/list"></ListView>

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Task"
            android:id="@+id/btn"></Button>
</LinearLayout>

In the class, I added a OnClickListener to the button, Then the app cannot be started

public class Test2 extends ListActivity {
    String[] list={"1","2","3"};
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new Button.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                            }
                                        });

                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, list));
    }
}

If I remove this

        btn.setOnClickListener(new Button.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                            }
                                        });

Then the app is completely work, but the button cannot handle event

How to add the listener to the button when the class extends ListActivity?

CL So
  • 3,647
  • 10
  • 51
  • 95

3 Answers3

2

you are not set any content in you activity.

add this line

setContentView(R.layout.activity_main);

just replace activity_main with your xml file name in your ListActivity.

public class Test2 extends ListActivity {
    String[] list={"1","2","3"};
    Button btn;

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

        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new Button.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                            }
                                        });

                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, list));
    }
}
Mr X
  • 1,053
  • 2
  • 11
  • 24
0

See This Add buttons to a listactivity

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String[] values = {"One", "Two", "Three"};
            setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));
            setContentView(R.layout.activity_main);

            btn = (Button) findViewById(R.id.btn);

            btn.setOnClickListener(new Button.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {

                                                    Toast.makeText(MainActivity.this, "working fine", Toast.LENGTH_LONG).show();
                                                }
                                            });
        }
Community
  • 1
  • 1
Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
0
Replace your button with this.

 <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Task"
            android:focusable="false"
            android:editable="false"
            android:id="@+id/btn">
</Button>
rahul shah
  • 2,086
  • 2
  • 11
  • 14