无法在onOptionsItemSelected(MenuItem item)方法中解析Textview

时间:2018-07-19 09:14:13

标签: android user-interface

这是MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView myTxt = findViewById(R.id.view);


       }



    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.test, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.toast:
                      myTxt.setText("what a yummy toast!");

                return true;
            case R.id.nutella:

                return true;

            case R.id.milk:


            default:
                return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
        }

    }

}

这是菜单XML文件

<?xml version="1.0" encoding="utf-8"?>

<item android:title="@string/toast"
    android:id="@+id/toast"/>


<item android:title="@string/nutella"
    android:id="@+id/nutella"/>

<item android:title="@string/milk"
    android:id="@+id/milk"/>

每当我从菜单选项中选择一个项目时,我都希望处理myTxt,它是一个文本视图,问题是无法识别myTxt。我应该如何正确处理菜单? Here is an image for more explanation

2 个答案:

答案 0 :(得分:0)

您需要将 TextView myTxt 声明为 onCreate() 方法之外的全局变量

示例代码

public class MainActivity extends AppCompatActivity {

    TextView myTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         myTxt = findViewById(R.id.view);


       }



    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.test, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.toast:
                      myTxt.setText("what a yummy toast!");

                return true;
            case R.id.nutella:

                return true;

            case R.id.milk:


            default:
                return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
        }

    }

}

答案 1 :(得分:0)

myTxt在onCreate()中声明,因此您不能从此函数外部访问它。尝试在onCreate()之外声明它

public class MainActivity extends AppCompatActivity {
private TextView myTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myTxt = findViewById(R.id.view);


   }



public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.test, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.toast:
                  myTxt.setText("what a yummy toast!");

            return true;
        case R.id.nutella:

            return true;

        case R.id.milk:


        default:
            return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
    }

}

}