-1

我正在尝试使用 onCreateOptionsMenu 和 onOptionsItemSelected 调用其他活动,但我不知道无法调试并且它不执行操作,我有

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.login_settings, menu);º
    //return super.onCreateOptionsMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getGroupId();
    if(id == R.id.contactMenu) {
            Intent intent = new Intent(this, ContactActivity.class);
            this.startActivity(intent);
            return super.onOptionsItemSelected(item);
    }

    return true;
}

和菜单项

 <group
        android:id="@+id/contactMenu"
        android:checkableBehavior="single">
            <item
                android:id="@+id/contactSettings"
                android:icon="@drawable/correo"
                android:title="@string/ContactUs_settings" />
    </group>

太感谢了...

4

1 回答 1

0

像下面的代码一样更改并且工作正常。您必须使用 ItemId 来选择 menuItem

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId(); //you must use item id not group id
    if(id == R.id.contactSettings) {
            Intent intent = new Intent(this, ContactActivity.class);
            startActivity(intent);
    }
    return true;
}
于 2020-02-12T13:50:23.443 回答