0

I'm trying to use a ListView which is just like on this page:

http://developer.android.com/resources/tutorials/views/hello-listview.html

I want to use setContentView and then use the ListView which I put on that layout. Here's my code:

public class ListViewTrainingsActivity extends ListActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);    // It gives me error here?

  final String[] COUNTRIES = new String[] {
            "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
            "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
            "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
            "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
            "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
            "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
            "British Virgin Islands"};

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

  ListView lv = (ListView) findViewById(android.R.id.list);
  lv.setTextFilterEnabled(true);

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

}
  }

and I'm getting an error that says:

 E/AndroidRuntime(306): FATAL EXCEPTION: main
 E/AndroidRuntime(306): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yahya.ListViewTrainings/com.yahya.ListViewTrainings.ListViewTrainingsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
 E/AndroidRuntime(306):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 .
 .
 .
 E/AndroidRuntime(306): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
 E/AndroidRuntime(306):     at android.app.ListActivity.onContentChanged(ListActivity.java:245)
 E/AndroidRuntime(306):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
 E/AndroidRuntime(306):     at android.app.Activity.setContentView(Activity.java:1647)
 E/AndroidRuntime(306):     at com.yahya.ListViewTrainings.ListViewTrainingsActivity.onCreate(ListViewTrainingsActivity.java:17)

I hope anybody can tell me what I'm doing wrong.

user
  • 86,916
  • 18
  • 197
  • 190
yahya
  • 4,810
  • 3
  • 41
  • 58
  • looks like this problem to me: http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error `ListView lv = (ListView) findViewById(android.R.id.list);` remove `android` and the reference that s added by eclipse to the `import` list – Orkun Feb 09 '12 at 14:33

2 Answers2

2

In your layout main.xml you must have a ListView element with the id(extending ListActivity requires this):

android:id="@android:id/list"

Also because you have extended the ListActivity class you can get the ListView by calling the method getListView():

ListView lv = getListView();
user
  • 86,916
  • 18
  • 197
  • 190
2

Change your listView id with this android:id="@android:id/list" check if this helps.

Deepak
  • 1,989
  • 1
  • 18
  • 20