androidx.appcompat.view.menu.ActionMenuItemView 无法转换为 android.view.MenuItem
androidx.appcompat.view.menu.ActionMenuItemView cannot be cast to android.view.MenuItem
我在尝试在特定片段处于活动状态时从工具栏的菜单中隐藏某些项目时遇到问题。 (编辑:在这种情况下,工具栏未设置为 ActionBar)
我一直在四处寻找解决方案,发现您可以设置 MenuItem 的可见性,我尝试实现这个,但我收到一条错误消息,指出 MenuItem 实际上是一个 ActionMenuItemView。
我不知道这是否与我使用的是 Androidx 有关。之后,我尝试用 ActionMenuItemView 替换 MenuItem 以查找是否有可以隐藏视图但找不到的方法。
这是我的代码(在这种情况下,我想在备份片段处于活动状态时隐藏工具栏上的刷新图标):
public class HomeActivity extends AppCompatActivity {
private Toolbar toolbar2;
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new ReportFragment();
final Fragment fragment3 = new BackupFragment();
final Fragment fragment4 = new SettingFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Setting up the Toolbar
toolbar2 = (Toolbar)findViewById(R.id.toolbar_2);
toolbar2.inflateMenu(R.menu.test_menu);
//Setting bottom navaigation view
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigation_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
toolbar2.setTitle("Submitted Reports");
return true;
case R.id.navigation_report:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
toolbar2.setTitle("Templates Reports");
return true;
case R.id.navigation_backup:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
toolbar2.setTitle("Upload Reports");
MenuItem refresh = (MenuItem)findViewById(R.id.action_refresh);
refresh.setVisible(false);
return true;
case R.id.navigation_setting:
fm.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
toolbar2.setTitle("Settings");
return true;
}
return false;
};
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.shid.form.UI.HomeActivity"
android:fitsSystemWindows="true"
android:background="@color/primary">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_menu"
app:popupTheme="@style/Theme.Popup"
app:theme="@style/Theme.GuidelinesCompat.Toolbar"
app:title="Submitted Reports" />
<!-- Fragments Container -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="25dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</FrameLayout>
<!-- app:layout_constraintTop_toBottomOf="@+id/appBarLayout"-->
<!-- Bottom Navigation View -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我发现了问题出在哪里,出于某种原因重写 onCreateOptionsMenu 对我不起作用,所以我找到了另一种方法:
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(false);
所以在我的例子中,因为我想在特定片段处于活动状态时隐藏项目,所以我创建了两个方法并根据片段调用它们:
private void hideMenuItems(){
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(false);
toolbar2.getMenu().findItem(R.id.action_search).setVisible(false);
}
private void revealMenuItems(){
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(true);
toolbar2.getMenu().findItem(R.id.action_search).setVisible(true);
}
我在尝试在特定片段处于活动状态时从工具栏的菜单中隐藏某些项目时遇到问题。 (编辑:在这种情况下,工具栏未设置为 ActionBar)
我一直在四处寻找解决方案,发现您可以设置 MenuItem 的可见性,我尝试实现这个,但我收到一条错误消息,指出 MenuItem 实际上是一个 ActionMenuItemView。
我不知道这是否与我使用的是 Androidx 有关。之后,我尝试用 ActionMenuItemView 替换 MenuItem 以查找是否有可以隐藏视图但找不到的方法。 这是我的代码(在这种情况下,我想在备份片段处于活动状态时隐藏工具栏上的刷新图标):
public class HomeActivity extends AppCompatActivity {
private Toolbar toolbar2;
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new ReportFragment();
final Fragment fragment3 = new BackupFragment();
final Fragment fragment4 = new SettingFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Setting up the Toolbar
toolbar2 = (Toolbar)findViewById(R.id.toolbar_2);
toolbar2.inflateMenu(R.menu.test_menu);
//Setting bottom navaigation view
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigation_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
toolbar2.setTitle("Submitted Reports");
return true;
case R.id.navigation_report:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
toolbar2.setTitle("Templates Reports");
return true;
case R.id.navigation_backup:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
toolbar2.setTitle("Upload Reports");
MenuItem refresh = (MenuItem)findViewById(R.id.action_refresh);
refresh.setVisible(false);
return true;
case R.id.navigation_setting:
fm.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
toolbar2.setTitle("Settings");
return true;
}
return false;
};
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.shid.form.UI.HomeActivity"
android:fitsSystemWindows="true"
android:background="@color/primary">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_menu"
app:popupTheme="@style/Theme.Popup"
app:theme="@style/Theme.GuidelinesCompat.Toolbar"
app:title="Submitted Reports" />
<!-- Fragments Container -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="25dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</FrameLayout>
<!-- app:layout_constraintTop_toBottomOf="@+id/appBarLayout"-->
<!-- Bottom Navigation View -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我发现了问题出在哪里,出于某种原因重写 onCreateOptionsMenu 对我不起作用,所以我找到了另一种方法:
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(false);
所以在我的例子中,因为我想在特定片段处于活动状态时隐藏项目,所以我创建了两个方法并根据片段调用它们:
private void hideMenuItems(){
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(false);
toolbar2.getMenu().findItem(R.id.action_search).setVisible(false);
}
private void revealMenuItems(){
toolbar2.getMenu().findItem(R.id.action_refresh).setVisible(true);
toolbar2.getMenu().findItem(R.id.action_search).setVisible(true);
}