2017-02-23 68 views
-2

菜单未创建...代码有什么问题? 是什么原因?Android中未创建菜单

public class myListViewActivity extends AppCompatActivity { 


     ArrayList<Country> countries; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       countries = new ArrayList<Country>(); 


      myAdapter adapter = new myAdapter(this,countries); 
      ListView listView = new ListView(this); 
      registerForContextMenu(listView); 
      listView.setAdapter(adapter); 
      setContentView(listView); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 

      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.options, menu); 
      return true; 

     } 

    } 

// XML文件.............. // -------------------- options。 XML ------------------------------

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/open_item_menu" 
     android:title="Open" /> 
    <item 
     android:id="@+id/delete_item_menu" 
     android:title="close" /> 
</menu> 
+0

PLZ发布R.menu.options的代码文件。 –

+0

<?xml version =“1.0”encoding =“utf-8”?> <项目机器人:ID = “@ + ID/delete_item_menu” 机器人:标题= “关闭” /> – Khawar

回答

0
//----------------------- Try It ------------------------ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu, menu);//Menu Resource, Menu 

     return true; 
    } 

    //------------------------------------------------------------- 

    <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.sample.app.MainActivity"> 


     <item 
      android:id="@+id/action_refresh" 
      android:icon="@mipmap/ic_home_white" 
      android:orderInCategory="101" 
      android:title="Home" 
      android:visible="false" 
      app:showAsAction="ifRoom" /> 
    </menu> 
+0

不工作! :( – Khawar