Errors after building apk
Asked Answered
K

3

8

Action bar views (share action provider and searchview) crashes after generating signed apk. When i debug app from android studio it's working but when i generating signed apk and install app to device i getting errors like this :

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)

or

 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)

I'm using last versions of support libraries and toolbar. (by setting 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;
    }

How can i fix this? Like is said above, i get errors only after signing apk.

Kilar answered 6/12, 2014 at 19:33 Comment(5)
possible duplicate of What is a Null Pointer Exception, and how do I fix it?Nuclide
Find out what's null and make sure that it isn't.Nuclide
@Nuclide i know that (SearchView) searchItem.getActionView(); return null. but i don't know why i'm getting this error after generating apk..not when debuggingKilar
and this not duplicate of that question..Kilar
Are you exporting the support library?Nuclide
P
4

It happens because you have not configured your progaurd appropiately. In you build.gradle under buildTypes and 'release' change runProguard true to runProguard false and generate the singed APK and test. If it works well, try learning to configure progaurd appropriately :)

Painterly answered 6/12, 2014 at 20:5 Comment(2)
progaurd might remove code that it thinks is not used, but your application might actually need it. in that case you have to identify the class and add it to prograurd configuration like -keep public class <MyClass> Read more on developer.android.com/tools/help/proguard.htmlPainterly
Better to use -keep class android.support.v7.widget.SearchView { *; } instead of disabling proguardSinasinai
M
16

In proguard-rules.pro, add the following line to exclude the SearchView class: '-keep class android.support.v7.widget.** { *; }' and then create the build. I resolved it like this.

Majesty answered 25/5, 2015 at 13:14 Comment(3)
Thank you man, you saved my day, I was about to give up on proguard due to this error :)Mulvey
A much better answer than just disabling proguard. Thank you very much!Parrotfish
Thank you, very helpful.Triboluminescence
P
4

It happens because you have not configured your progaurd appropiately. In you build.gradle under buildTypes and 'release' change runProguard true to runProguard false and generate the singed APK and test. If it works well, try learning to configure progaurd appropriately :)

Painterly answered 6/12, 2014 at 20:5 Comment(2)
progaurd might remove code that it thinks is not used, but your application might actually need it. in that case you have to identify the class and add it to prograurd configuration like -keep public class <MyClass> Read more on developer.android.com/tools/help/proguard.htmlPainterly
Better to use -keep class android.support.v7.widget.SearchView { *; } instead of disabling proguardSinasinai
S
1

This works for me,

Add the following line to proguard-rules.pro file located inside app folder:

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

Sutlej answered 13/7, 2017 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.