0

I am making a sample Lollipop's app based where I put a Material Toolbar and to manage it I am using a menu 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AREAHotspot.AppBarOverlay">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:title="@string/app_name"
        app:menu="@menu/menu_action"
        app:navigationIcon="@drawable/area"
        style="@style/Widget.MaterialComponents.Toolbar.Primary" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/hotspots_tabs"
        android:layout_width="409dp"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="335dp"
        android:background="?attr/colorPrimary"
        app:tabSelectedTextColor="#77AC1C"
        app:tabTextColor="#FFFFFF" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/area_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="384dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I am using also ViewPager2 with a TabLayout and two fragments for two tabs. In Main Activity I put:

public class MainActivity extends AppCompatActivity {

    private Method method;
    private Menu menuAction;
    private MaterialToolbar mtoolbar;
    
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_action, menu);
        this.menuAction = menu;

        return super.onCreateOptionsMenu(menu);
    }   
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //< create >
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mtoolbar = findViewById(R.id.topAppBar);
        setSupportActionBar(mtoolbar);
        //</ create>

        //< get elements >
        TabLayout area_tabs = findViewById(R.id.hotspots_tabs);
        ViewPager2 area_viewpager = findViewById(R.id.area_viewpager);
        //</ get elements >

        // an adapter needed to load fragments into viewpager
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        area_viewpager.setAdapter(adapter);

        new TabLayoutMediator(area_tabs, area_viewpager,
            new TabLayoutMediator.TabConfigurationStrategy() {
                @Override
                public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                    if (position == 0)
                        tab.setText("Hotspot devices");
                    if (position == 1)
                        tab.setText("Functions");
                }
        }).attach();
    }


    @Override
    protected void onStart () {
        super.onStart();

        method = null;
        try {
            [...]
            actualAPState = ((Boolean) method.invoke(...));
            if (actualAPState) {
                this.menuAction.getItem(0).setIcon(getResources().getDrawable(R.drawable.green_24dp));
            } else {
                this.menuAction.getItem(0).setIcon(getResources().getDrawable(R.drawable.red_24dp));
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        method.setAccessible(true);
    }   

}

so in this way I should have already menu instance got from inflating in onCreateOptionsMenu because it's designed into xml so in fragment where I have just a fab button I can call a method and change the icon state depending of method's result , but when I go install the app and it starts I get this error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.getItem(int)' on a null object reference 

maybe I could have missed something...I used also setHasOptionsMenu(true); in fragments but I guess that method is related to fragment content itself, not the toolbar...why it returns that error? Any help explanation would be appreciated... thanks in advance to all!!

Cheers! Luigi

Luigino
  • 545
  • 2
  • 5
  • 21

0 Answers0