Dave
Dave

Reputation: 157

Android: Adding a ListActivity to TabHost results in ClassCastException

Using the example provided by Google, I've sucessfully created a TabHost.

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, SearchListActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("search").setIndicator("Search",
                      res.getDrawable(R.drawable.icon))
                  .setContent(intent);

    tabHost.addTab(spec);

and

public class SearchListActivity extends ListActivity

However if I try to add a ListActivity to the TabHost, it results in a ClassCast exception:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.companyName.appName/com.companyName.appName.MainActivity}:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.companyName.appName/com.companyName.appName.SearchListActivity}:
java.lang.ClassCastException: android.app.Application

Is it not possible to add a ListActivity to a TabHost?

Upvotes: 0

Views: 1346

Answers (2)

RonzyFonzy
RonzyFonzy

Reputation: 698

I did this the following way:

public class ActivityTab extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator("Title 1")
            .setContent(new Intent(this, ListActivity_1.class)));

    tabHost.addTab(tabHost.newTabSpec("tab2")
            .setIndicator("Title 2")
            .setContent(new Intent(this, ListActivity_2.class)));
}}

For the classes ListActivity_1 and ListActivity_2 you just write this:

    public class ListActivity_1 extends ListActivity { 
    private String[] mStrings = {"Item 1", "Item 2", "Item 3"};
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (Application_VarnePoti)getApplication();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mStrings));
    }}

Upvotes: 1

Franco
Franco

Reputation: 7475

it's possible to add any activity into a TabActivity, did you declare the ListActivity in the manifest? can you put the TabActivity declaration?

Upvotes: 0

Related Questions