Android tutorials -- tab layout. The error message is tabhost. setcurrenttab (getintent ());

Source: Internet
Author: User

This evening, I used the android development network to create a tab layout instance, but it was always unsuccessful!

No reason. The tutorials on the official website are all wrong !!

The result was tested by me without passing the test! I also share my thoughts with you!

Original Tutorial:



Running environment:

Eclipse 3.4

For Android 1.6


If you follow the above steps, it will not succeed! There is still a lack of important files and code. I will post the important steps:

1. Start a new project namedHellotabwidget

. (Unchanged)

2. Create three Java:ArtistsActivity


, And

Public class artistsactivity extends activity {<br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview = new textview (this); <br/> textview. settext ("this is the artists tab"); <br/> setcontentview (textview); <br/>}< br/>}

We only need to replace the artists in "this is the artists tab" with albums, songs

3. In Res/drawable/(some are drawable-mdpi), create a new role. If you do not have a PNG image, you can find it under (Android installation directory/platforms/Android-1.6/data/RES/drawable!

The important thing is that you do not need to createIc_tab_artists.xml, no need! I think!

Please let me analyze for you: when tab layout is created, the selected icon background will become white. If no selected icon is selected, the background is dimmed. The icon we are looking for is light gray.


4. Openres/layout/main.xml

File and insert the following :( unchanged)


And make it extendTabActivity

:.Package COM. hello. tabview; <br/> Import android. app. tabactivity; <br/> Import android. content. intent; <br/> Import android. content. res. resources; <br/> Import android. OS. bundle; <br/> Import android. widget. tabhost; <br/> public class hellotabwidget extends tabactivity {<br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> resources res = getresources (); // resource object to get drawables <br/> tabhost = gettabhost (); // The activity tabhost <br/> tabhost. tabspec spec; // reusable tabspec for each tab <br/> intent; // reusable intent for each tab <br/> // create an intent to launch an activity for the tab (to be reused) <br/> intent = new intent (). setclass (this, artistsactivity. class); <br/> // initialize a tabspec for each tab and add it to the tabhost <br/> spec = tabhost. newtabspec (""). setindicator ("article", <br/> res. getdrawable (R. drawable. ic_tab_artists) <br/>. setcontent (intent); <br/> // tabspec android. widget. tabhost. tabspec. setcontent (intent) <br/> // newtabspec ("mytag") is the following setcurrenttabbytag ("mytag") <br/> // setindicator (Label, icon ); label is the text shown above <br/> tabhost. addtab (SPEC); <br/> // do the same for the other tabs <br/> intent = new intent (). setclass (this, albumsactivity. class); <br/> spec = tabhost. newtabspec ("B "). setindicator ("photo", <br/> res. getdrawable (R. drawable. ic_tab_albums) <br/>. setcontent (intent); <br/> tabhost. addtab (SPEC); <br/> intent = new intent (). setclass (this, songsactivity. class); <br/> spec = tabhost. newtabspec ("C "). setindicator ("song", <br/> res. getdrawable (R. drawable. ic_tab_songs) <br/>. setcontent (intent); <br/> tabhost. addtab (SPEC); <br/> // tabhost. setcurrenttab (0); // The default tab starts from 0 and sets the current tab <br/> tabhost. setcurrenttabbytag ("B"); <br/>}< br/>}

6. Another key point is to modify the androidmanifest. xml file and <activity Android: Name = ".HelloTabWidget

"......> ...... </Aciti.pdf> Add the following code:

<Activity Android: Name = ". albumsactivity "<br/> Android: Label =" @ string/app_name "<br/> Android: theme =" @ Android: style/theme. notitlebar "> <br/> </activity> <br/> <activity Android: Name = ". artistsactivity "<br/> Android: Label =" @ string/app_name "<br/> Android: theme =" @ Android: style/theme. notitlebar "> <br/> </activity> <br/> <activity Android: Name = ". songsactivity "<br/> Android: Label =" @ string/app_name "<br/> Android: theme =" @ Android: style/theme. notitlebar "> <br/> </activity>

7. Run the project to obtain the image. (Maybe you have some difference)



Well, it succeeded.

However, I still found a problem, but it was not solved.

Question: If you press the Home Page icon

And then enter the application. The tab remains in the released status.

However, if you press the back key

And then, the tab is restored to the default state.

No answer is found. Which of the following can answer me !!





Related Article

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: 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.