这是我正在做的程序我在main.xml中创建了两个项目,即“关于我们”和“首选项”,我想在我的程序中使用但是当我尝试使用inflate方法访问main.xml时在MenuInflater中它无法找到我的资源。
package com.sagar.firstapp;
import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[]={"StartingPoint","TextPlay","Email","Camera","Data","example5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(Menu.this,android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese=classes[position];
try {
Class ourclass=Class.forName("com.sagar.firstapp."+cheese);
Intent ourintent=new Intent(Menu.this,ourclass);
startActivity(ourintent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowup=getMenuInflater();
blowup.inflate(R.menu.main, menu);/*Can't find my main.xml resource*/
return true;
}
}
这是我的main.xml代码
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="About Us"
android:id="@+id/aboutus"
android:numericShortcut="1"
android:alphabeticShortcut="a"/>
<item
android:title="Preferences"
android:id="@+id/preferences"
/>
R.java(生成的行)
public static final int aboutus=0x7f09001d;
public static final int preferences=0x7f09001e;
public static final class menu {
public static final int main=0x7f080000;
}
答案 0 :(得分:0)
在res /菜单文件夹下创建main.xml
<强> main.xml中强>
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1"
android:title="About Us"/>
<item android:id="@+id/item2"
android:title="Preferences"/>
</menu>
以及inflate()
onCreateOptionsMenu(menu 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);//Menu Resource, Menu
return true;
}