java - Android:从菜单项开始新 Activity

标签 java android android-activity menu

我正在尝试为我的应用程序打开第二个屏幕。用户必须单击菜单项,然后屏幕必须打开。但我尝试的总是忽略 case 语句后的规则。

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.OpenScreen:  startActivity(new Intent(this, SecondScreenActivity.class));;
                            break;
    }
    return true;

我还在标签内将 Activity 添加到 AndroidManifest.xml。

我们将不胜感激


解决方案

我在新类的构造函数中有 finish()

最佳答案

对我来说,您的 Java 代码是正确的,而且对我来说也是如此。

但是,当我学习 Android 时,当我开始一项新 Activity 时,我遇到了与您相同的问题,没有任何反应,没有任何错误,只是无法正常工作。

在我的例子中,我忘记在 AndroidManifest.xml 文件中添加 Activity 。或许你也一样。

你的 AndroidManifest 文件应该包含类似的东西

<activity
    android:name=".SecondScreenActivity" >
</activity>

这告诉系统 SecondScreenActivity 退出。

http://developer.android.com/guide/topics/manifest/activity-element.html他们说:

All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.

希望对你有帮助

关于java - Android:从菜单项开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9030213/

相关文章:

java - 使用java(netbeans),windows在sqlite中进行数据排序

java - 二次方的导数

android - 在 Android SDK 中-如何在 EditText 中用户按下的像素位置找到单词

android - 启动 Activity 出错

Android资源匮乏操作

java - 使用 Model.setNsPrefix() 定义前缀后不能使用带前缀的 URI?

java - Neo4j Hello World 教程和 webadmin

android - 将 sqlite 数据库从设备复制到 PC

android - 了解 Android BLE ScanSettings 参数

android - To Fragment or not to Fragment - 针对 Activity 的嵌套 fragment 。为什么我应该使用多个 Activity ?