Nullpointerexception仅在签名APK

时间:2017-05-06 15:38:24

标签: android nullpointerexception searchview

我有一个包含以下代码的片段:

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);

            }
        });

如果我从AndroidStudio运行它,那么一切都很好。但是每个从Play商店下载应用程序的人都会崩溃:

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

2 个答案:

答案 0 :(得分:1)

这是我发现的:

我必须添加

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

到proguard.pro

答案 1 :(得分:-1)

下面的代码有效

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