2015-11-08 32 views
1

我創建tabHost一個標籤,每個標籤下的下一個活動: MainActivity.classgetActionBar()空一tabHost

TabHost tabHost = getTabHost(); 

    this.setNewTab(this, tabHost, "tab1", R.drawable.icon_blackberry_config, new Intent().setClass(this, HiepActivity.class)); 
    this.setNewTab(this, tabHost, "tab2", R.drawable.icon_windows_config,new Intent().setClass(this, BlackBerryActivity.class)); 
    this.setNewTab(this, tabHost, "tab3", R.drawable.icon_apple_config,new Intent().setClass(this, AppleActivity.class)); 
    this.setNewTab(this, tabHost, "tab4", R.drawable.icon_android_config,new Intent().setClass(this, AndroidActivity.class)); 
    tabHost.getTabWidget().setStripEnabled(false); 

    tabHost.setCurrentTab(2); 
} 
private void setNewTab(Context context, TabHost tabHost, String tag, int icon, Intent in){ 
     TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag); 
     tabSpec.setIndicator(getTabIndicator(tabHost.getContext(), icon)); // new function to inject our own tab layout 
     tabSpec.setContent(in); 
     tabHost.addTab(tabSpec); 
    } 

private View getTabIndicator(Context context, int icon) { 
     View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null); 
     ImageView iv = (ImageView) view.findViewById(R.id.imageView1); 
     iv.setImageResource(icon); 
     return view; 
    } 

而且HiepActivity下,我想顯示出與片段輕掃標籤:

public class HiepActivity extends Activity implements ActionBar.TabListener { 

// Declaring our tabs and the corresponding fragments. 
ActionBar.Tab bmwTab, fordTab, toyotaTab; 
Fragment bmwFragmentTab = new TopRatedFragment(); 
Fragment toyotaFragmentTab = new GamesFragment(); 
Fragment fordFragmentTab = new TopRatedFragment(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_ACTION_BAR); 
    setContentView(R.layout.hieptab); 

    ActionBar actionBar = getActionBar(); 
    Log.v("error", actionBar+""); 

但是在HiepActivity中調用getActionBar()時總是爲空。 我想要的是在tabhost活動(MainActivity)下有一個swipetab(HiepActivity)。爲什麼以及如何解決這個問題?謝謝。 更新: 我發現它是因爲MainActivity是使用操作欄,所以HiepActivity沒有ActionBar.So它不可能解決這個問題。非常感謝。

+1

getActionBar沒有在你的應用程序解決,認爲可能不使用清單中正確的主題,還有一個件事使用AppcompatActivity被如果可能的話。 – Androider

+0

我試過改變主題,但仍然是錯誤 – edcon

+1

主要的是,你也應該使用getSupportActionBar() – Androider

回答

0

的問題是在當前類的情況下嘗試使用類背景.getActionBar()

+0

我這樣認爲,MainActivity是使用操作欄,所以MainActivity中的tabhost中的活動不能使用操作欄。但如何解決這個問題? – edcon

+0

你可以試試:this.getActionBar() –