views:

123

answers:

2

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();
        }
      });
}

}

A: 

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.

jjb
Yes, my manifest includes the activities for both tabs. The project displays fine when Tab1 extends Activity but crashes when I extend ListActivity.
HappyGeisha
Do you have the log of the crash? Can you add the layouts?
jjb
My problem was solved, Thanks for your help :)
HappyGeisha
+1  A: 

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

binnyb
That worked! Thank you! I was using a unique id for my listview. Do you know why ListActivity only allows @android:id/list ??
HappyGeisha
i don't know! ha
binnyb