我正在尝试为我的应用程序打开第二个屏幕。用户必须单击菜单项,然后屏幕必须打开。但我尝试的总是忽略 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/