简体   繁体   中英

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

使用ListActivity时, ListView的ID必须为@android:id/list

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM