Android Studio使用XML配置选项菜单选择事件发生的空指针异常

这个问题困扰我差不多四个小时,就是因为我很习惯性地在Activity中使用findViewById方法实例化对象导致调用onOptionsItemSelected方法时候一直产生空指针异java.lang.NullPointerException: Attempt to invoke interface method 'int  android.view.MenuItem.getItemId()' on a null object reference,当我点击选项菜单中的选项时候App运行直接闪退;

 public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    return super.onOptionsItemSelected(item);
}

原来的代码如下

public class Menu_definedByXml extends AppCompatActivity {

    private TextView tex_word;
    private TextView tv_menu;
    private MenuItem menu_first;
    private MenuItem menu_second;
    private MenuItem menu_third;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.menu_defined_by_xml);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tex_word = findViewById(R.id.test_word);
        menu_first = findViewById(R.id.menu_first);
        menu_second =findViewById(R.id.menu_second);
        menu_third = findViewById(R.id.menu_third);
    }


    /**
     * 创建选择菜单
     * @param menu The options menu in which you place your items.
     *
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 绑定自定义菜单选择事件
     * @param item The menu item that was selected.
     *
     * @return ture
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId==menu_first.getItemId()){
            Toast.makeText(this,"设置字体大小",Toast.LENGTH_SHORT).show();
        }

        if (itemId==menu_second.getItemId()){
            Toast.makeText(this,"选择了菜单选项",Toast.LENGTH_SHORT).show();
        }

        if (itemId==menu_third.getItemId()){
            Toast.makeText(this,"设置字体颜色",Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }
}

我将在Activity使用findViewById()方法实例化的对象删除并且修改了onOptionsItemSelected()方法中的条件就可以正常运行了

最后的代码如下:

public class Menu_definedByXml extends AppCompatActivity {

    private TextView tex_word;
    private TextView tv_menu;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.menu_defined_by_xml);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tex_word = findViewById(R.id.test_word);
    }


    /**
     * 创建选择菜单
     * @param menu The options menu in which you place your items.
     *
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 绑定自定义菜单选择事件
     * @param item The menu item that was selected.
     *
     * @return ture
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId==R.id.menu_first){
            Toast.makeText(this,"设置字体大小",Toast.LENGTH_SHORT).show();
        }

        if (itemId==R.id.menu_second){
            Toast.makeText(this,"选择了菜单选项",Toast.LENGTH_SHORT).show();
        }

        if (itemId==R.id.menu_third){
            Toast.makeText(this,"设置字体颜色",Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }
}

之后运行就正常了

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值