开发者

How to call TabActivity getTabHost() from another activity? to avoid NullPointerException

开发者 https://www.devze.com 2023-04-02 20:04 出处:网络
Im in a process of learning android. Want to implement tabbed browsing functionality; For this purpose I have following activity

Im in a process of learning android. Want to implement tabbed browsing functionality; For this purpose I have following activity

public class CustomList extends ListActivity {
....

and inside this (and other activities I want to put the tabs)

for example:
Resources res = getResources();

   **TabActivity ta = new TabActivity();**
        TabHost host = **ta.getTabHost();**  // this line returns **NullPointerException**

        host.addTab(host.newTabSpec("first")
                .setIndicator("First")
                .setContent(new Intent(this, First.class)));
These lines are included inside OnCreate().

Probably it would be better to have separate activity with tab definitions and just call it in every other activities. But, Im not sure how to do this.

Please for suggestion to solve this issu开发者_Python百科e. Thank you in advance.


I think what you want is to have multiple activity within a single tab.

In order to do this, you have to first create an Activity which extend TabActivity, and initialize its tabs with activities extending TabGroupActivity.

public class BaseActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  addTab("home",home_tab,tab_sales_icons,new Intent(this, TabGroupSalesActivity_.class));
  }
}

public class TabGroupSalesActivity extends TabGroupActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //Start the first Activity of the group
  Intent newIntent = new Intent(this, Sales_.class);
  newIntent.putExtra("XMLObject",xml);
  startChildActivity(newIntent);
  }
}

Here is a link with more informations on how to have multiple activities in a TabActivity

0

精彩评论

暂无评论...
验证码 换一张
取 消