TabActivity和getTabHost()在我的日食中不起作用。如何解决这些问题?

时间:2013-04-05 03:32:07

标签: android-tabhost tabactivity

这是我的代码

这里有一些问题

  1. 为什么TabActivity在我的日食中是划痕?
  2. getTabHost()收到错误?任何人都可以修复此代码吗?
  3. 我会尝试向androidhive.com学习,然后我会看到这个教程,但是当我在eclipse中编写它时,它失败了。

    package com.uavero.androidtablayout;
    import android.os.Bundle;
    import android.app.TabActivity;
    import android.content.Intent;
    import android.view.Menu;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    
    public class TabLayout extends TabActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
    
        TabHost tabHost = getTabHost();
    
        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photo_tab));
        Intent photosIntent = new Intent(this, PhotosActivity.class);
        photospec.setContent(photosIntent);
    
        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("Songs");
        songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_song_tab));
        Intent songsIntent = new Intent(this, SongsActivity.class);
        songspec.setContent(songsIntent);
    
        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_video_tab));
        Intent videosIntent = new Intent(this, VideosActivity.class);
        videospec.setContent(videosIntent);
    
        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(videospec); // Adding videos tab
          }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_tab_layout, menu);
        return true;
              }
    
         }
    

1 个答案:

答案 0 :(得分:0)

我可以解决这个问题。 Android 3.0和高级版不支持TabActivity, 或者如果您正在使用较低版本但仍然发生错误,则更改此行代码; - TabHost tabHost = getTabHost();, import.app.TabActivity,和 TabHost tabHost =(TabHost)findViewById(R.id.tabhost)。

相关问题