0

我正在从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>

这是菜单类

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

    }

}


  }

文本播放类

  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();
}


  }
4

2 回答 2

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>
于 2013-02-03T11:42:17.150 回答
0

您的 .Splash 活动中包含 <category android:name="android.intent.category.LAUNCHER" /> 意图过滤器。

这意味着这是您运行应用程序时启动的活动。

编辑 :

在您的 Splash.java 中,您正在使用意图过滤器“com.example.android.MainActivity”启动活动

这不是你的菜单活动..你的菜单活动有意图过滤器“com.example.android.Menu”

所以只要改变

Intent openMainActivityClass = new Intent("com.example.android.MainActivity");

Intent openMainActivityClass = new Intent("com.example.android.Menu");

于 2013-02-03T11:59:00.170 回答