MainActivity. java
package tk.serdarsevimli.devicemanager.activities;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import tk.serdarsevimli.devicemanager.R;
import tk.serdarsevimli.devicemanager.tools.util.ToastUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
try {
getMenuInflater().inflate(R.menu.fav_menu, menu);
}catch (ClassCastException e){
ToastUtils.showLong(MainActivity.this,e.getMessage());
}
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
app_bar_layout. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
tools:context=".activities.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_orange_dark"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fav_menu. xml
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context=".activities.MainActivity">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_favorite"
android:title="@string/action_favorite"
android:icon="@android:drawable/ic_secure"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:icon="@android:drawable/ic_menu_send"
app:showAsAction="never"/>
</menu>
Для скриншота нажмите
Я получаю следующие сообщения об ошибках. * .lang.ClassCastException: androidx.appcompat.widget.ActionMenuView не может быть приведен к androidx.appcompat.view.menu.ListMenuItemView в androidx.appcompat.view.menu.MenuAdapter.getView (MenuAdapter. java: 101) в androidx. appcompat.view.menu.MenuPopup.measureIndividualMenuWidth (MenuPopup. java: 161) в androidx.appcompat.view.menu.StandardMenuPopup.tryShow (StandardMenuPopup. java: 174) в androidx.appMenup.Puup.Puup.Puup.Puup.Penup.Puup.Pu. show (StandardMenuPopup. java: 208) в androidx.appcompat.view.menu.MenuPopupHelper.showPopup (MenuPopupHelper. java: 290) в androidx.appcompat.view.menu.MenuPopupHelper.tryShow (MenuPopupH28: 10 .upupH28). 177) на androidx.appcompat.widget. ActionMenuPresenter $ OpenOverflowRunnable.run (ActionMenuPresenter. java: 796) в android .os.Handler.handleCallback (Обработчик. java: 873) в android .os.Handler.dispatchMessage (Обработчик. java: 99) на android .os.Looper.l oop (Looper. java: 201) на android .app.ActivityThread.main (ActivityThread. java: 6820) на java .lang. refle.Method.invoke (собственный метод) в com. android .internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit. java: 547) в com. android .internal.os.ZygoteInit.main (ZygoteInit . java: 922) I / i.devicemanage: вызван System.exit, статус: 2 I / AndroidRuntime: виртуальная машина завершает работу с кодом результата 2, очистка пропущена. Отключен от целевой виртуальной машины, адрес: «localhost: 8600», транспорт: «сокет»