android - 使用android和SHOWasAction属性使菜单栏出现错误

我需要有一个菜单或操作栏,当用户使用该应用程序时必须显示这些菜单或操作栏,但问题是菜单项不会仅在我单击模拟器时显示菜单菜单,否则任何人都可以帮助我吗?

test.java

package com.example.test;

import com.example.test.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class test extends Activity {

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


    }

    @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_activity_menu, menu);
        return true;
    }

}

mani_activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="settings"/>
    <item
        android:id="@+id/twitter"
        android:orderInCategory="2"
        android:showAsAction="always"
        android:title="twitter"/>
    <item
        android:id="@+id/facebook"
        android:orderInCategory="3"
        android:showAsAction="always"
        android:title="facebook"/>
    <item 
        android:id="@+id/refresh"
        android:title="refresh"
        android:showAsAction="always"
        android:orderInCategory="4"
        />


</menu>

表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".test"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

style.xml
<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

最佳答案

尝试过您的代码。它的工作没有任何问题

您在mani_activity_menu.xml中犯了拼写错误?


setHasOptionsMenu(true);//在oncreate中
item.setEnabled(false);//将其添加到oncreated选项中

  package com.example.exammmm;



   import android.os.Bundle;

   import android.app.Activity;
   import android.view.Menu;

    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.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

   }


(来源:mediafire.com)

阅读-----> http://developer.android.com/guide/topics/ui/actionbar.html

https://stackoverflow.com/questions/18922219/

相关文章:

java - 按给定因子缩放线段

c++ - Count.h :10:5: error: ISO C++ forbids declar

java - 错误 :class interface or enum expected

java - 编写一个程序,询问产品代码,产品数量并汇总所有价格

ios - drawRect :中的EXC_BAD_ACCESS

java - 在PHP中获取Java编译错误

java - 在其他类中访问主类中的公共(public)变量

compiler-errors - Java类路径问题

c++ - 发布版本中的 Visual C++ 6 编译错误

c++ - C++中的运行时错误字符串声明