发布版本

时间:2017-10-06 12:08:53

标签: android searchview android-proguard

在尝试获取onCreateOptionsMenu中对Custom SearchView类的引用时,我遇到了崩溃。通常这工作正常,但在加载版本apk时,它崩溃了。我尝试将这些行添加到我的proguard但它仍然崩溃:

# Android support
-keep public class com.mine.mobile.mypackage.usercontrol.SearchViewCustom
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; } 

以下是代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mMenu = menu;
        getMenuInflater().inflate(R.menu.menu_invest, menu);
        final MenuItem searchItem = menu.findItem(R.id.action_invest_search);

        if (searchItem != null) {
            mSearchView = (SearchViewCustom) MenuItemCompat.getActionView(searchItem);

            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }

        mPresenter =  new BaseActivityPresenterImpl(this,mSearchView,mMenu,searchItem);
        mPresenter.setupActionBar(NONE,false);

        return super.onCreateOptionsMenu(menu);
    }

这是日志

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mine.mobile.mypackage.usercontrol.d.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
!   1   at com.mine.mobile.mypackage.activity.home.a.onCreateOptionsMenu(BaseActivity.java:94)
2   at android.app.Activity.onCreatePanelMenu(Activity.java:3232)
3   at android.support.v4.app.q.onCreatePanelMenu(FragmentActivity.java:362)
4   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
5   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
6   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
7   at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
8   at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
9   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
10  at android.view.Choreographer.doCallbacks(Choreographer.java:683)
11  at android.view.Choreographer.doFrame(Choreographer.java:616)
12  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
13  at android.os.Handler.handleCallback(Handler.java:751)
14  at android.os.Handler.dispatchMessage(Handler.java:95)
15  at android.os.Looper.loop(Looper.java:154)
16  at android.app.ActivityThread.main(ActivityThread.java:6247)
17  at java.lang.reflect.Method.invoke(Native Method)
18  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
19  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_delete"
        android:title="@string/delete"
        android:icon="@drawable/ic_delete_white"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_done"
        android:title="@string/done"
        android:icon="@drawable/ic_done_white"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_refresh_white"
        android:visible="false"
        android:title="@string/refresh_menu"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_invest_search"
        android:icon="@drawable/ic_search_white"
        android:visible="false"
        android:title="@string/action_search"
        app:actionViewClass="com.mine.mobile.mypackage.usercontrol.SearchViewCustom"
        app:showAsAction="always|collapseActionView" />
</menu>

这不是什么是NullPointerException的副本,我该如何解决? ......这个问题包含的范围过于广泛

1 个答案:

答案 0 :(得分:1)

我在正确的轨道上,但显然我的专业防守语法不正确(我使用谷歌的例子here:)。这虽然有效:

-keep public class com.mine.mobile.mypackage.usercontrol.SearchViewCustom {
  public *;
}