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