0

I'm building user guide with MaterialShowcaseView library. I plan to guide user through all toolbar options menu items in fragment (Theme.AppCompat with no action bar is used). Thus I need to reference to each options menu item as view. But unfortunately I receive NullPointerException in onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    getActivity().getMenuInflater().inflate(R.menu.account_summary_menu, menu);
    View item = menu.findItem(R.id.action_filter).getActionView();
    if(item == null)  Log.d("TAG", "null");

    super.onCreateOptionsMenu(menu, inflater);
}

This works well in onOptionsItemSelected method but is not preferred. Any help strongly appreciated.

UPD: Fragment code

public class AccountSummary extends Fragment {

public AccountSummary() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_account_summary, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getActivity().setTitle(getResources().getStringArray(R.array.drawer_menu)[0]);

}

@Override
public void onResume() {
    super.onResume();

}

//Create options menu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.account_summary_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_filter:
            showPopup();
            return true;

        case R.id.document_share:
            try {
                ExportData.exportSummaryAsCsv(getContext(), prepareCsvData());
            } catch (IOException e) {
                e.printStackTrace();
            }
        default:
            return super.onOptionsItemSelected(item);
    }

}

}

UPD2 It seams that view is created before menu inflation is finished. I guess I can use workaround like Thread.sleep() but is there any more elegant way?

burzakovskiy
  • 157
  • 2
  • 9
  • Hi @burzakovskiy i am facing same issue, my requirement is also same. I am using "mreram:showcaseview" library for user guide. In this i have to pass View object to highlight that part. I am facing problem to find View object of option menu icon (Three dots on top Right Corner) please help me out if you have any idea. – amit semwal Jul 14 '20 at 14:17

1 Answers1

0

Since the fragment equivalent is already giving you a MenuInflater, probably try doing this..

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

May be in your case, getActivity() may be throwing a null pointer because the onCreate method of Activity is called first, and before it finishes onCreateOptionsMenu is called.

Suraj Kumar Sau
  • 438
  • 6
  • 10