我正在从newboston学习android ..我想先显示一个菜单,在本教程中显示..我已经完成了所有相同但不知道为什么菜单活动不会出现..帮我在哪里我做错了
这是我的代码
android manfiest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 2nd activity -->
<activity
android:name="com.example.android.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.android.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- 3rd Activity Test -->
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.android.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TesxtPlay"
android:label="@string/app_name" >
</activity>
</application>
这是menuClass
public class Menu extends ListActivity {
//declaring above so both methods can access these
String classes[] = {"MainActivity","TextPlay","example2",
"example3","example4","example5","example6",};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(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];
Class ourClass;
try {
ourClass = Class.forName("com.example.android." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
textPlayclass
public class TextPlay extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button chkCmd = (Button) findViewById(R.id.bResults);
final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
final EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.display);
passTog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(passTog.isChecked()){
input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
}else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
}
}
溅
public class Splash extends Activity {
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle iloveyou) {
// TODO Auto-generated method stub
super.onCreate(iloveyou);
setContentView(R.layout.splash);
ourSong = new MediaPlayer().create(Splash.this, R.raw.kalimba);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
//starting activity
Intent openMainActivityClass = new Intent("com.example.android.MainActivity");
startActivity(openMainActivityClass);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
答案 0 :(得分:0)
菜单未显示,因为您没有对其进行充气。
在您的Menu类中添加以下代码行:
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
这假设您的菜单存储为menu.xml
文件夹/res/menu
。
修改强>
您的menu.xml
应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/itemAboutUs"
android:icon="@android:drawable/ic_menu_info_details"
android:title="About Us">
</item>
<item
android:id="@+id/itemPreferences"
android:icon="@android:drawable/ic_menu_preferences"
android:title="Preferences">
</item>
</menu>
答案 1 :(得分:0)
您的.Splash活动有
<category android:name="android.intent.category.LAUNCHER" />
意图过滤器。
这意味着这是您运行应用时启动的活动。
编辑:
在您的Splash.java中,您使用intent过滤器启动活动“”com.example.android.MainActivity“
这不是你的菜单活动..你的菜单活动有意图过滤器“com.example.android.Menu”
所以只需改变
Intent openMainActivityClass = new Intent("com.example.android.MainActivity");
到
Intent openMainActivityClass = new Intent("com.example.android.Menu");