Cannot resolve symbol ‘item’

huangapple go评论86阅读模式
英文:

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,

huangapple
  • 本文由 发表于 2020年10月2日 22:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64173484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定