构建apk后的错误

时间:2014-12-06 19:33:51

标签: android android-actionbar-compat android-toolbar

生成签名的apk后,操作栏视图(共享操作提供程序和searchview)崩溃。当我从Android工作室调试应用程序它正在工作,但当我生成签名apk并安装应用程序到设备我得到这样的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQueryHint(java.lang.CharSequence)' on a null object reference
            at tm.veriloft.ertir.ui.SearchActivity.onCreateOptionsMenu(Unknown Source)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
            at android.support.v4.app.o.onCreatePanelMenu(Unknown Source)
            at android.support.v7.app.e.a(Unknown Source)
            at android.support.v7.app.g.a(Unknown Source)
            at android.support.v7.widget.as.a(Unknown Source)
            at android.support.v7.internal.a.a.j(Unknown Source)
            at android.support.v7.internal.a.b.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ah.a(android.content.Intent)' on a null object reference
            at tm.veriloft.ertir.ui.BlogViewActivity.onCreateOptionsMenu(Unknown Source)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
            at android.support.v4.app.o.onCreatePanelMenu(Unknown Source)
            at android.support.v7.app.e.a(Unknown Source)
            at android.support.v7.app.g.a(Unknown Source)
            at android.support.v7.widget.as.a(Unknown Source)
            at android.support.v7.internal.a.a.j(Unknown Source)
            at android.support.v7.internal.a.b.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我使用支持库和工具栏的最新版本。 (通过设置setSupportActionBar(mToolbar))。

SearchView xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ertir="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="@string/title_activity_search"
        ertir:showAsAction="always"
        ertir:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

Java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        mSearchView.setQueryHint(getString(R.string.search_hint));
        mSearchView.setFocusable(true);
        mSearchView.setIconified(false);
        mSearchView.requestFocusFromTouch();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                mSearchView.clearFocus(); //Hide keyboard
                search(query, searchTypeArray.get(searchTypeSpinner.getSelectedItemPosition()).getType());
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                if (searchTypeArray.get(searchTypeSpinner.getSelectedItemPosition()).getType().equals("blogs"))
                    searchAfter(1100, query, searchTypeArray.get(searchTypeSpinner.getSelectedItemPosition()).getType());
                else
                    search(query, searchTypeArray.get(searchTypeSpinner.getSelectedItemPosition()).getType());
                return false;
            }
        });
        return true;
    }

我该如何解决这个问题?如上所述,我只有在签署apk后才会收到错误。

3 个答案:

答案 0 :(得分:16)

在proguard-rules.pro中,添加以下行以排除SearchView类: &#39; -keep class android.support.v7.widget。** {*; }&#39;然后创建构建。我这样解决了。

答案 1 :(得分:4)

这是因为您尚未适当地配置progaurd。在您build.gradlebuildTypes和'发布'将runProguard true更改为runProguard false并生成已烧录的APK并进行测试。如果它运作良好,请尝试学习适当配置progaurd:)

答案 2 :(得分:1)

这对我有用,

将以下行添加到位于app文件夹中的proguard-rules.pro文件中:

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