Attention points for the use of Android--tabhost

Source: Internet
Author: User

Oneself in the process of using tabhost because of time and oneself understand not deep reason, after acquiring to Tabhost AddTab operation often encounter null pointer exception, here do a summary

1. When we directly let activity inherit from Tabactivty:

    • You need to specify the ID of a tabhost control in the layout file
    • Use Gettabhost () in activity to get the Tabhost control object

2. When our activity does not inherit directly from Tabactivity, nor does it intent fill fragment

    • The Tabhost control needs to be obtained through Findviewbyid, and the layout does not specify an ID
    • You need to initialize the call Tabhost.setup () before using Tabhost. Important thing to say three times, if you do not call this function, a null pointer exception will appear
    • Call Tabhost.setup () to initialize
    • Call Tabhost.setup () to initialize
    • Call Tabhost.setup () to initialize

In this case, it is not possible to embed other activity into the current tab page directly at the time of AddTab, or the system will prompt for NULL pointer exceptions such as:

Tabhost. AddTab(Tabhost. Newtabspec("Tab1"). Setindicator("First"). SetContent(New Intent (This,act1. Class)));Tabhost. AddTab(Tabhost. Newtabspec("TAB2"). Setindicator("Second"). SetContent(New Intent (this,act2. Class)));Tabhost. AddTab(Tabhost. Newtabspec("Tab3"). Setindicator("Third"). SetContent(New Intent (this,act3. Class)));

However, a suggestion would be made:

to call ‘publicvoid setup(LocalActivityManager activityGroup)‘?

This tells us to invoke the Tabhost method. But the parameters of the method are only passed
Getlocalactivitymanager () function, this function is activitygroup only, so please look at the third article

3. When we need to populate fragment with intent when our activity does not directly inherit from tabactivity

    • Activity needs to inherit from Activitygroup
    • Get Tabhost through Findviewbyid
    • Call Tabhost.setup (Getlocalactivitymanager ()) before using Tabhost;

Therefore, in general, if you want to use Tabhost, then directly inherit from Tabactivity is the most convenient method, but you need to specify in the layout file Tabhost control's ID is android:id/tabhost (note case), And get to the Tabhost object directly through the Gettabhost () method in activity

Attention points for the use of Android--tabhost

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.