英文:
Cannot resolve symbol 'item'
问题
@Override
public boolean onOptionsItemSelected(MenuItem item){
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
switch (item.getItemId()){
case R.id.Item1:
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2:
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3:
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
英文:
I'm trying to make my menu items go to different activities, so I tried to override the onOptionsItemSelcted on every activity, but I'm getting this error.
also getting "Constant expression required" on every case "R.id.ItemX".
@Override
public boolean onOptionsItemSelected(MenuItem menu){
switch (item.getItemId()){ // ERROR IS HERE ON THE WORD 'item', Cannot resolve symbol 'item'.
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
case R.id.Item1: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
答案1
得分: 1
使用这个:
menu.getItemId()
而不是这个:
item.getItemId()
示例代码
@Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) { // 这里的错误在于单词 'item',其余部分正常工作。
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
case R.id.Item1:
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2:
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3:
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
英文:
Use this
menu.getItemId()
Instead of this
item.getItemId()
SAMPLE CODE
@Override
public boolean onOptionsItemSelected(MenuItem menu){
switch (menu.getItemId()){ // ERROR IS HERE ON THE WORD 'item', THE REST WORKS FINE.
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
case R.id.Item1:
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2:
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3:
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
答案2
得分: 0
使用这个:
menu.getItemId()
这将返回项目的ID,
英文:
use this
menu.getItemId()
this will return you the item id,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论