【问题标题】:Menu not showing up in Activity - Android菜单未显示在活动中 - Android
【发布时间】:2016-06-21 03:07:12
【问题描述】:

我已经为 Android 中的菜单编写了代码,但它现在显示在我的活动中,这是我的代码

我这里只显示我的相关代码,

Manifest.xml

<uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >

MainActivity.Java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
        // Some action here

        }
    }
}

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.androidex1.MainActivity" >

   <item android:id="@+id/item1"
       android:title="one" />
    <item android:id="@+id/item2"

        android:title="two" />
    <item android:id="@+id/item3"

        android:title="three" />
    <item android:id="@+id/item4"

        android:title="four" />

</menu>

之前尝试过的步骤

Menu Items are not showing on Action Bar

结果:

尝试过但仍然没有出现菜单

Android options menu not displaying

结果:

菜单仍然没有出现。

Android Menu not showing up

结果:

从未添加过该属性。

Option Menu does not appear in Android

结果:

super.onCreateOptionsMenu(menu); is also added in the code but still the menu's don't appear.

我的问题 我只想看到菜单而不是操作栏,可能是什么问题?我没有收到任何错误,主要问题是我看不到菜单本身。

【问题讨论】:

  • 好的,我建议使用Android Studio!你看到我的帖子了吗?我编辑它...
  • @Dario 在 Studio 中尝试过相同的操作,但不明白发生了什么?还是看不到简单的菜单
  • 你能发布一些你的应用程序的图片吗?添加更多信息,以便我们了解问题所在!我简单地创建一个新项目,使用您的代码创建一个 menu.xml,将菜单添加到活动中,如果我按下手机 Galaxy S3 中的菜单按钮,我会看到菜单的 3 个选项。与 android studio 中的 nexus 7 的 AVD 相同。建议使用最新版本SDK更新的android studio再试一次。
  • 发布您的结果图片和您期望的图片,如果可以的话!

标签: android menu


【解决方案1】:

在我的情况下,我解决了它

android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">

activity_main.xml

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:background="@color/colorPrimary"/>

【讨论】:

    【解决方案2】:

    1.首先在Activity布局中添加工具栏

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <androidx.appcompat.widget.Toolbar
            android:id="@id/toolbar"
            android:layout_width="match_parent"
            app:title="About"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent" />
    </com.google.android.material.appbar.AppBarLayout>
    

    2.在Activity中添加工具栏

    (Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);        
    

    3.从您的 Activity 中扩展 AppCompatActivity

    4.Override onCreateOptionsMenu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    【讨论】:

      【解决方案3】:

      您在活动的膨胀菜单之前致电super.onCreateOptionsMenu(menu); 所以你需要像这样删除这个功能:

       @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
          }
      

      【讨论】:

        【解决方案4】:

        第 1 步: 将此添加到您的工具栏 xml 文件中

         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:popupTheme="@style/Theme.AppCompat.Light"
        

        第 2 步: 将此添加到您的 java 代码中

        Toolbar toolbar = findViewById(R.id.order_management_toolBar);setSupportActionBar(toolbar);

        就是这样。

        【讨论】:

          【解决方案5】:

          如果上述答案无效,请确保您使用的是相同的工具栏 ID。

          layout_app_bar.xml

          <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize">
          

          activiy_main.xml

          <include
              android:id="@+id/app_bar"
              layout="@layout/layout_app_bar" />
          

          在java文件中:

          不要打电话:

          Toolbar tb = findViewById(R.id.toolbar);
          

          请致电:

          Toolbar tb = findViewById(R.id.app_bar);
          

          【讨论】:

            【解决方案6】:

            由于我完全使用 kotlin 进行编码,因此您需要将其添加到 onCreate 或 onCreateView 中

            setHasOptionsMenu(true)
            

            【讨论】:

              【解决方案7】:

              这里有同样的问题,调试了几个小时后,我只是尝试创建另一个布局 xml 文件并更新活动以使用新布局。并从旧布局复制粘贴,更改一些 ID。不知道为什么,但它有效

              【讨论】:

                【解决方案8】:

                您需要做的就是首先将您的工具栏与布局连接起来。

                 android.support.v7.widget.Toolbar toolbar=findViewById ( R.id.toolbar );
                 setSupportActionBar ( toolbar );
                

                然后

                    @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    MenuInflater inflater=getMenuInflater ();
                    inflater.inflate ( R.menu.city_menu,menu );
                    return true;
                }
                

                【讨论】:

                  【解决方案9】:

                  就我而言,我忘记设置支持操作栏(用 kotlin 编写)

                  override fun onCreate(savedInstanceState: Bundle?) {
                      super.onCreate(savedInstanceState)
                      setContentView(R.layout.activity_student_home)
                      setSupportActionBar(toolbar) // after adding this line menu shown
                  

                  【讨论】:

                    【解决方案10】:

                    我有同样的问题....在 tge 结束时,我尝试了所有代码,结果发现它仅在我更改 Java 类中的扩展时才有效... 尝试使用默认的 AppCompatActivity

                    【讨论】:

                    • 这应该改写为正确的答案。在目前的状态下,这个答案看起来更像是一条评论。
                    【解决方案11】:

                    您必须从 AppCompatActivity 而不是 Activity 扩展

                    【讨论】:

                    • 您可以发布一些代码作为示例,说明如何按照您的建议改进您的答案。
                    • 这是不言自明的。而不是: public class MainActivity extends Activity { ... } 这样做: public class MainActivity extends AppCompatActivity { ... }
                    【解决方案12】:

                    尝试更改模拟器。有时模拟器会发生故障以阻止工作。

                    【讨论】:

                    • 你能否提供更多细节,因为这个答案看起来不太有希望
                    • 有时您创建的模拟器会出现故障,这会导致您创建的应用程序无法正确执行。一旦我所做的只是在重新运行我的应用程序之前更改模拟器,就会发生在我身上。它奏效了。
                    【解决方案13】:

                    我也遇到了同样的问题。 这是一种样式

                    在我在 android studio 中创建的项目中,我有 style.xml 文件: 有一条线。

                    <resources>
                    
                        <!-- Base application theme. -->
                        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                            <!-- Customize your theme here. -->
                        </style>
                    
                        <style name="AppTheme.NoActionBar">
                            <item name="windowActionBar">false</item>
                            <item name="windowNoTitle">true</item>
                        </style>
                    
                        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
                    
                        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
                    
                    </resources>
                    

                    在我的 manifest.xml 中(您没有显示完整的清单文件)。 其中一项活动(我想在其中显示菜单)设置了以下行:

                    <activity
                        android:name=".UsersListActivity"
                        android:label="@string/title_activity_users_list"
                        android:theme="@style/AppTheme.NoActionBar">
                    </activity>
                    

                    所以当我将 AppTheme.NoActionBar 更改为 AppTheme.AppTheme 并且现在它可以工作时:

                    <activity
                        android:name=".UsersListActivity"
                        android:label="@string/title_activity_users_list"
                        android:theme="@style/AppTheme.AppTheme">
                    </activity>
                    

                    现在它可以工作了。

                    【讨论】:

                      【解决方案14】:

                      我不清楚,您是否看到了您的操作栏但没有看到其中的菜单,或者您根本没有看到操作栏?如果它是最新的,请尝试在清单中的 &lt;application&gt; 标记中应用支持操作栏的样式。例如:

                      android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
                      

                      【讨论】:

                      • 我没有看到 Actionbar 事实上我没有在我的活动中添加任何 Actionbar
                      • 因为您在 Java 类中扩展了 Activity。如果扩展 Activity,则不能使用 Theme......DarkActionBar。现在 ActionBarActivity 它已被弃用,使用 AppCompatActivity 并使用 Toolbar 这是新的 ActionBar
                      • 我想扩展 Activity 而不是 AppCompatActivity。解决这个问题的方法是什么?
                      • 尝试添加 onCreate requestWindowFeature(Window.FEATURE_ACTION_BAR);
                      • 我应该在哪里调用这个函数?
                      【解决方案15】:

                      我尝试了您的代码,并且在我的手机中一切正常! 唯一不同的想法是

                       @Override
                          public boolean onOptionsItemSelected(MenuItem item) {
                              // Handle item selection
                              switch (item.getItemId()) {
                                  case R.id.item1:
                                      //DoSomething();
                                      Toast.makeText(getApplicationContext(), "Hello 1", Toast.LENGTH_LONG).show();
                                      return true;
                                  case R.id.item2:
                                      //DoSomething2();
                                      Toast.makeText(getApplicationContext(), "Hello 2", Toast.LENGTH_LONG).show();
                                      return true;
                      
                                  case R.id.item3:
                                      //DoSomething3();
                                      Toast.makeText(getApplicationContext(), "Hello 3", Toast.LENGTH_LONG).show();
                                      return true;
                                  default:
                                      return super.onOptionsItemSelected(item);
                              }
                          }
                      

                      你确定目录和菜单名是对的吗?

                      getMenuInflater().inflate(R.menu.main, menu);
                      

                      确保目录是menu,文件menu是main.xml 当它是自动生成的 Android Studio 调用它 main_activity_menu.xml 时,请确保充气器中的名称是正确的,因为我使用您的代码创建了一个新的测试应用程序并且我看到了菜单!

                      如果您想添加带有图标等菜单项的 ActionBar,请阅读此Google.developer guide

                      另一个Menus guide from google.developer 如果不需要,不带操作栏 编码愉快!

                      【讨论】:

                      • 为什么通常的方法不起作用?我们为什么要扩展 AppCompatActivity ?我仍然想扩展 Activity。抱歉,这不能回答我的问题。
                      • 您想要一个 ActionBar 到您的 Activity 对吗?如果你只扩展 Activity,你就不能有一个 ActionBar。要获得您想要的内容,请按照我发布的代码进行操作,或者更改您的问题,因为它可能是错误的。
                      • See this,怎么实现一个菜单而不扩展 AppCompat Activity ?
                      • 所以你只需要一个菜单​​。好的,编辑你的帖子不清楚。你得到一些错误?发布日志 如果你触摸手机上的菜单按钮会发生什么???什么都没有显示?无论如何尝试扩展 AppcompatActivity 不仅是 Activity 它不会破坏你的手机别担心!
                      • 请参阅我的问题中的“我的问题”部分,我已对其进行了编辑。
                      【解决方案16】:

                      你膨胀了错误的菜单文件:

                      getMenuInflater().inflate(R.menu.main, menu);

                      您的菜单文件必须根据您的代码命名为 main.xml。

                      【讨论】:

                        猜你喜欢
                        • 2011-05-31
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-05-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多