HappyGeisha
HappyGeisha

Reputation: 565

Android: Error when using ListActivity inside Tab Layout

My apologies in advance if this is a repeat question, I looked all over and couldn't find any solution to help me.

I have followed the android dev tutorial for creating a tabbed UI that uses a separate activity for each tab.

And I got it working just fine. Until...

I am attempting to put a ListView inside one of the tabbed activities (Tab1). To get the usability I want, I find that I need to extend ListActivity. Thats when I get the 'Force close' error. It displays just fine when I extend regular Activity.

Here is my nonfunctional Tab1.java code:

public class Tab1 extends ListActivity {
    ListView lv;
    String[] times = {
        "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM",
            "12:00 AM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM",
            "5:00 PM", "6:00 PM", "7:00 PM"
    };

    /** Called when the activity is first created. */@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);

        lv = (ListView) findViewById(R.id.ListView_Tab1);
        lv.setAdapter(new ArrayAdapter < String > (this, R.layout.list_item, R.id.times,
            times));
        lv.setOnItemClickListener(new OnItemClickListener() {@
            Override
            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();
            }
        });
    }
}

Upvotes: 0

Views: 3254

Answers (2)

james
james

Reputation: 26271

the ID of your ListView must be @android:id/list when using a ListActivity

Upvotes: 1

jjb
jjb

Reputation: 3570

Did you declare the new Activity in your manifest? If you try and create a tab for an Activity not declare there, it will crash.

Upvotes: 0

Related Questions