Android 从tabhost活动中的菜单中展开操作栏

Android 从tabhost活动中的菜单中展开操作栏,android,android-actionbar,android-tabhost,Android,Android Actionbar,Android Tabhost,在我的应用程序中,我在Tabhost活动中为操作栏充气。在这里,即使我使用onCreateOptions菜单(菜单菜单菜单)充气,操作栏也不会显示。在下面我已经发布了我的代码 TabhostActivity.xml <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:i

在我的应用程序中,我在Tabhost活动中为操作栏充气。在这里,即使我使用
onCreateOptions菜单(菜单菜单菜单)
充气,操作栏也不会显示。在下面我已经发布了我的代码

TabhostActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/appblue">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

</TabHost>

在这里,Actionbar不显示,但除TabhostActivity之外的所有其他活动都工作正常

首先定义appcompatActivity的私有变量,如下所示

private AppCompatActivity myact;
然后在oncreate方法中添加以下行

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    myact.setSupportActionBar(toolbar);
对于低于11级的api

myact.getActionBar(toolbar);

我认为这应该可以解决问题。看,这个问题的根本原因是您的类扩展了TabActivity,而不是AppcompatActivity或Activity。因此,我们必须特别提到是否显示Actionbar。我建议您使用Android Studio的标准Tabsliderview来设计您的应用程序,它更易于使用,而且结构良好

如果我添加那一行,我会在那一行上得到空指针异常。您使用的是哪个版本的支持库?哪个平台使用的是eclipse或studio?我使用的是android studio suupport library v7
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    myact.setSupportActionBar(toolbar);
myact.getActionBar(toolbar);