菜单没有出现在活动 - 安卓

问题描述:

我已经写了代码,菜单的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. 

我的问题 我想看到的仅仅是菜单不动作条,可能是什么问题呢?我没有收到任何错误,主要问题是我无法看到菜单本身。

+0

您使用哪个IDE? – Dario

+0

@Dario Eclipse IDE。 –

+0

好的,我建议使用Android Studio! 你看到我的帖子吗?我编辑它... – Dario

我试过了你的代码,并且在我的手机中所有的作品! 唯一不同的认为是

@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); 

确保该目录菜单和文件菜单的main.xml 当自动生成的Android工作室称之为main_activity_menu.xml,所以一定要在充气的名字是正确的,因为我做一个新的测试应用程序与您的代码,我看到菜单!

如果你想添加菜单项的动作条一样的图标

另一个Menus guide from google.developer没有动作条,如果不需要 编码愉快阅读本Google.developer guide

+0

为什么通常的方法不起作用?为什么我们应该扩展AppCompatActivity?我仍然想扩展活动。对不起,这并没有回答我的问题。 –

+0

你想要一个ActionBar到你的活动权利? 如果只扩展Activity,则不能有ActionBar。 为了得到您想要的内容,请遵循我发布的代码,或者更改您的问题,因为这可能是错误的。 – Dario

+0

[见此](http://www.javatpoint.com/android-option-menu-example),如何在不扩展AppCompat Activity的情况下实现菜单? –

目前还不清楚,你是否看到你的动作栏,没有看到菜单,或者你根本看不到动作栏?如果是最新版本,请尝试在清单中的<application>标签中应用支持操作栏的样式。例如:

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

我没有看到Actionbar Infact我没有添加任何Actionbar到我的活动 –

+0

你有没有尝试过使用我建议的主题? –

+0

是的,但仍然无法正常工作。 –

会人为增加了错误的菜单文件:

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

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

+0

它是'main.xml',对不起将编辑我的问题。 –

我遇到了同样的问题。 这是一个造型

在我的项目在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> 

现在它可以工作。

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

+0

你可以提供更多的细节,因为这个答案看起来并不那么有希望 – Ibo

+0

有时你创建的模拟器出现故障,这导致你的创建的APP不适当的实现。有一次,我所做的只是在重新运行我的APP之前更改模拟器。有效。 –

您必须从AppCompatActivity扩展而不是活动

+1

您可以发布一些代码作为示例,了解如何执行您的建议以改进您的答案。 – rsz