英文:
Why is the menu I created not working with a new Intent
问题
我是安卓开发的新手。
我创建了一个 MenuItem,在点击时启动一个新的活动(SettingsActivity)。我没有错误,一切都正常工作,只是当我点击创建的菜单时,它什么也不做。
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.visualizer_menu, menu);
return true;
}
public boolean onOptionSelected(MenuItem item){
int itemThatWasClicked = item.getItemId();
if (itemThatWasClicked == R.id.action_settings){
Intent settingsActivityIntent = new Intent(this, SettingsActivity.class);// 使用显式 intent 打开新活动
startActivity(settingsActivityIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
我尝试在 onOptionSelected()
中添加了一个 Toast,但没有作用,菜单没有响应。
现在我在 SettingsActivity.java
中有以下代码:
package android.example.com.visualizerpreferences;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == android.R.id.home){
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
}
以下是我的菜单资源文件 visualizer_menu.xml
内容:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
英文:
I am new to android development.
I created a MenuItem to start a new activity (SettingsActivity) when clicked. I have no errors and everything is working fine except that when I click the created menu, it does not do anything.
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.visualizer_menu, menu);
return true;
}
public boolean onOptionSelected(MenuItem item){
int itemThatWasClicked = item.getItemId();
if (itemThatWasClicked == R.id.action_settings){
Intent settingsActivityIntent = new Intent(this, SettingsActivity.class);// using explicity intent to open new activity
startActivity(settingsActivityIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
I have tried adding a Toast in onOptionSelected()
, it didn't work. the menu is not just responding
I have the following code in my SettingsActivity.java for now
package android.example.com.visualizerpreferences;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == android.R.id.home){
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
}
Below is my visualizer_menu.xml in my menu resource file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
答案1
得分: 1
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
英文:
replace your
public boolean onOptionSelected(MenuItem item){
to
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
答案2
得分: 0
菜单项创建:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
点击监听器:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 处理选项选择
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
英文:
Menu item create :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Click listeners :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论