Search code examples
androidnullpointerexceptionsearchview

Nullpointerexception only in signed APK


I have a fragment that contains the following code:

SearchView searchView;

    @Override
    public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.searchship, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);

        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    menu.findItem(R.id.action_sort).setVisible(false);

            }
        });

If I run this from AndroidStudio then everything is fine. But everyone who downloads the app from the playstore crashes:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnSearchClickListener(android.view.View$OnClickListener)' on a null object reference

Solution

  • Here is what I found:

    I have to add

    -keep class android.support.v7.widget.SearchView { *; }
    

    to proguard.pro