1

I've followed this tutorial but when I try to run the application I get Unfortunately HelloListView has stopped The IDE gives no warnings or errors.

My HelloViewListActivity.java looks like this:

public class HelloListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] countries = getResources().getStringArray(R.array.countries_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

        ListView lv = getListView();
        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();
            }
        });
    }
}

My strings.xml and list_item.xml are both identical to the tutorial.

Logcat log here What is it that I am doing wrong?

darren
  • 18,845
  • 17
  • 60
  • 79
  • 2
    It seems your list_content_simple.xml is not proper. Verify one more time. – kosa Jan 25 '12 at 19:35
  • @thinksteep the list_content_simple.xml is not mentioned in the tutorial there. I did find it out the SDK folders and added it to my res/layout but still same error. – darren Jan 25 '12 at 19:45
  • The tutorial you are referring is little bit fragmented. See my answer now. I have included link for working example tutorial, you may ignore database part if you don't want. – kosa Jan 25 '12 at 19:51
  • Use [logcat](http://developer.android.com/guide/developing/tools/logcat.html) to get more information about a crash. Or on a device, use the Catlog app from the market. – Julian Fondren Jan 25 '12 at 20:08

2 Answers2

2

You have to add setContentView(R.layout.yourlayout) after the super.onCreate(savedInstanceState);

the R.layout.yourlayout should look like this ( in folder res/layout ) :

<?xml version="1.0" encoding="utf-8"?>
<ListView 
  android:id="@android:id/list"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
louiscoquio
  • 10,638
  • 3
  • 33
  • 51
  • Thanks. Adding the ListView within a LinearLayout fixed my issue. It's just strange that in 2 tutorials I followed none of them mention this step. – darren Jan 25 '12 at 20:05
1

When you extend ListActivity, you need to have an xml which contains id as @android:id/list, then in your activity you need to setContentView(R.layout.yourxmlName); Here is tutorial.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • When I have a ListActivity I must always have something with '@android:id/list' ? Can I not change ID to anything else like '@android:id/list2' ? – darren Jan 25 '12 at 20:06
  • no, if you want to use a custom list ( or a custom id ), you have to use an `Activity` instead of a `ListActivity`, and then get the list with a `findViewById(R.id.thelistid)` – louiscoquio Jan 25 '12 at 20:10