11

I tried to update all the libraries, but i still got errors. I am able to run the app on the simulator, but when I export the APK and run it on a real android device, the app is not working.

MinSdkVersion is 8, targetSdkVersion is 19. Please help!!!

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:example="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_showTC"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Terms and Conditions"/>

    <item android:id="@+id/action_search"
          android:title="Search"
          android:orderInCategory="100"
          example:showAsAction="ifRoom|collapseActionView"
          example:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

SearchView

@Override
public boolean onOptionsItemSelected(MenuItem item){
    if(item.getItemId() == R.id.action_showTC){
        startActivity(new Intent(MainActivity.this, T_C.class));
        return true;
    }else{
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SupportMenuItem searchMenuItem = ((SupportMenuItem) menu.findItem(R.id.action_search));
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return super.onCreateOptionsMenu(menu);
}

manifest

    <activity android:name="com.agehk.queue.SearchResultsActivity"
        android:parentActivityName="com.agehk.queue.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="Search Queue"
    android:label="@string/app_name" />

Error

09-04 15:57:13.105: W/SupportMenuInflater(17184): Cannot instantiate class: android.support.v7.widget.SearchView
09-04 15:57:13.105: W/SupportMenuInflater(17184): java.lang.NoSuchMethodException: <init> [class android.content.Context]
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.Class.getConstructorOrMethod(Class.java:423)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.Class.getConstructor(Class.java:397)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.b(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.d.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.d.inflate(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.agehk.queue.MainActivity.onCreateOptionsMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.app.Activity.onCreatePanelMenu(Activity.java:2577)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v4.app.j.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.g.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.n.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.g.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.o.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:414)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:777)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:199)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:835)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer.doCallbacks(Choreographer.java:626)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer.doFrame(Choreographer.java:589)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:821)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Handler.handleCallback(Handler.java:730)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Looper.loop(Looper.java:150)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.app.ActivityThread.main(ActivityThread.java:5390)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.reflect.Method.invokeNative(Native Method)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.reflect.Method.invoke(Method.java:525)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at dalvik.system.NativeStart.main(Native Method)
09-04 15:57:13.105: D/AndroidRuntime(17184): Shutting down VM
09-04 15:57:13.105: W/dalvikvm(17184): threadid=1: thread exiting with uncaught exception (group=0x42f05500)
09-04 15:57:13.105: E/AndroidRuntime(17184): FATAL EXCEPTION: main
09-04 15:57:13.105: E/AndroidRuntime(17184): java.lang.NullPointerException
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.agehk.queue.MainActivity.onCreateOptionsMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.app.Activity.onCreatePanelMenu(Activity.java:2577)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v4.app.j.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.g.a(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.n.a(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.g.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.o.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:414)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:777)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:199)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:835)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer.doCallbacks(Choreographer.java:626)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer.doFrame(Choreographer.java:589)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:821)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Handler.handleCallback(Handler.java:730)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Looper.loop(Looper.java:150)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.app.ActivityThread.main(ActivityThread.java:5390)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at java.lang.reflect.Method.invoke(Method.java:525)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at dalvik.system.NativeStart.main(Native Method)
09-04 15:57:13.115: D/BatteryStatsImpl(445): noteStopWakeLocked com.google.android.gms isFromForeground = false
09-04 15:57:13.115: W/ActivityManager(445):   Force finishing activity com.agehk.queue/.MainActivity
We Rub Chan
  • 205
  • 1
  • 5
  • 14

8 Answers8

37

Add following code to proguard-rules.pro

-keep class android.support.v7.widget.SearchView { *; }
Bao Le
  • 16,643
  • 9
  • 65
  • 68
15

Have you enabled Proguard in your build? If so, you may want to ensure that the appcompat libraries are in the Proguard exclusion list (in proguard.cfg). A brute force approach is to keep all the support library classes with:

   -keep class android.support.v4.app.** { *; }
   -keep interface android.support.v4.app.** { *; }
   -keep class android.support.v7.app.** { *; }
   -keep interface android.support.v7.app.** { *; }

In my case, I had a class that extended the support library's SearchView so I added this to my proguard.cfg:

-keep public class * extends android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

The constructors are specifically mentioned to avoid the error:

java.lang.NoSuchMethodException: <init> [class android.content.Context]
RobD
  • 467
  • 3
  • 5
10

After long hours of research a simple solution of this problem i.e just add

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

in app/proguard/android.proguard file of AndroidStudio.

Cheers!

Sam Reyes
  • 329
  • 4
  • 9
5

I (also) got

Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.ao)' on a null object reference

when launching my release build (with proguard/minify enabled).

Adding this to the proguard rules fixed it:

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

This will keep the SearchView widget but will still allow proguard to throw away any other support library classes that you're not using, so you keep your release build nice and tidy.

Divisible by Zero
  • 2,616
  • 28
  • 31
5

Required minimum to proguard-rules.pro

-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}
  • This solution is still relevant today. Saw this bug pop-up again when updating from `com.android.tools.build:gradle:2.2.0` to `com.android.tools.build:gradle:2.2.2` and this minimal proguard rule fixed it. – Carmen Nov 07 '16 at 17:34
2

For AndroidX searchview proguard you have to use:

-keep class androidx.appcompat.widget.SearchView { *; }
Mujahid Khan
  • 1,712
  • 1
  • 18
  • 24
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Dwhitz Jul 08 '19 at 08:15
1

I had to add this and now it works smooth:

-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}
-keep interface android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}
Sid
  • 1,270
  • 2
  • 15
  • 30
0

For API before 11 you should initialize Action Bar Items in compatibility mode:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchMenuItem);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

Update:
So, I tried to reproduce it in API 8 emulator and on 4.4 KitKat. Unfortunately, my 2.3.8 device got bricked a while ago, so could not check in hardware with low API. What I can suggest you:
1. Check that SearchView is imported from android.support.v7.widget.SearchView;
2. Check menu resource is correct:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" <----------- init compatible namespace
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <item
        android:id="@+id/action_showTC"
        android:orderInCategory="100"
        android:title="@string/terms_and_conditions"
        app:showAsAction="never"/>  <----------- use compatible namespace

    <item android:id="@+id/action_search"
          android:title="@string/search"
          android:orderInCategory="100"  <---------- use different value
          app:showAsAction="ifRoom|collapseActionView"
          app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

3. return true from onCreateOptionsMenu if there's no underlying processing (Activity class is not subclassed)

UPDATE2: GOT IT! You probably extends from Activity class. Should be ActioBarActivity:

public class MainActivity extends ActionBarActivity {
...
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
  • Is it like this, but it stills got the same error. `MenuItem searchMenuItem = menu.findItem(R.id.action_search); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchMenuItem); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));` – We Rub Chan Sep 04 '14 at 16:14
  • Please take a look at http://developer.android.com/training/search/backward-compat.html#check-ver – Alexander Zhak Sep 04 '14 at 16:17
  • I do not know when I got wrong, but it stills no work. Can you please show me how to write the whole part. This question has perplex me a long time. Please!!! – We Rub Chan Sep 04 '14 at 16:35
  • @WeRubChan see update if it helps. I could not reproduce the issue from pieces of code you provided – Alexander Zhak Sep 04 '14 at 18:09
  • 1
    but **ActionBarActivity is deprecated.** – Banee Ishaque K May 31 '18 at 09:15