0

i've used the navigation bar template by android studio and tried to modify some things around - but not the inflaters are not working and I get a fatal error. Does anyone know what might be wrong? I've been trying to fix this for hours and nothing is working.

the XML file:

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

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

the java file:

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*
    1. ON CREATE, SET MAIN SCREEN TO

    here instead of setting the main screen with findViewById, we are using BINDING!
    this is part of android jetpack!
     */
    binding = ActivityMainBinding.inflate(getLayoutInflater());  // inflate method creates an instance of the binding class for this activity to use
    setContentView(binding.getRoot());   // getRoot: a reference to the root view and passing it to setcontentview to make it the active view on the screen
                                         // the root view for this app is activity_main.xml

    setSupportActionBar(binding.appBarMain.toolbar);

    // 2. ON CLICK LISTENER (open menu if clicked?)
    binding.appBarMain.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();
        }
    });

    // 3. Two widgets found in activity_main.xml
    DrawerLayout drawer = binding.drawerLayout;   // DrawerLayout: container for window content that allows for interactive "drawer" views to be pulled out from an edge of the window
    NavigationView navigationView = binding.navView;

    // 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)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
}
}

the logcat errors:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.busybee, PID: 9651
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.busybee/com.example.busybee.MainActivity}: android.view.InflateException: Binary XML file line #16 in com.example.busybee:layout/activity_main: Binary XML file line #19 in com.example.busybee:layout/content_main: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: android.view.InflateException: Binary XML file line #16 in com.example.busybee:layout/activity_main: Binary XML file line #19 in com.example.busybee:layout/content_main: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #19 in com.example.busybee:layout/content_main: Error inflating class fragment
     Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
        at android.widget.Spinner.setOnItemClickListener(Spinner.java:592)
        at com.example.busybee.ui.home.HomeFragment.onCreateView(HomeFragment.java:78)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1374)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2841)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2777)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3020)
        at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:394)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:260)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:142)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:295)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:274)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1073)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1001)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1267)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1267)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
        at com.example.busybee.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:53)
        at com.example.busybee.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:47)
        at com.example.busybee.MainActivity.onCreate(MainActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:8051)
        at android.app.Activity.performCreate(Activity.java:8031)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
W/System: A resource failed to call close. 
  • 1
    The last `Caused by:` line has the root issue: `RuntimeException: setOnItemClickListener cannot be used with a spinner.`, and that's happening `at com.example.busybee.ui.home.HomeFragment.onCreateView(HomeFragment.java:78)`. `Spinner` uses `OnItemSelectedListener` instead: https://stackoverflow.com/q/11322642. – Mike M. May 05 '22 at 23:55
  • @SomeoneLearning17 please add complete XML code. – M DEV May 06 '22 at 05:07
  • 1
    @MikeM. you were right! this was causing the fatal error, thank you! – SomeoneLearning17 May 06 '22 at 19:17

0 Answers0