liam xu
liam xu

Reputation: 3030

why android.support.v7.widget.SearchView can not be shown on the actionbar?

enter image description here

After I clicked the search button, nothing happened. The search box does not occur.Below is my menu.xml.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
            android:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.support.v7.widget.SearchView" />
    <item android:id="@+id/action_compose"
          android:icon="@drawable/ic_action_compose"
          android:title="@string/action_compose"
           android:showAsAction="never"  />
</menu>

Although the search widget was not shown on the action bar, but there was no exception thrown until I added the following code in method onCreateOptionsMenu .

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

The detailed exception is below.

01-19 14:17:00.222: W/ResourceType(2064): No package identifier when getting value for resource number 0x00000000
01-19 14:17:00.222: W/MenuInflater(2064): Cannot instantiate class: android.support.v7.widget.SearchView
01-19 14:17:00.222: W/MenuInflater(2064): java.lang.reflect.InvocationTargetException
01-19 14:17:00.222: W/MenuInflater(2064):   at java.lang.reflect.Constructor.newInstance(Native Method)
01-19 14:17:00.222: W/MenuInflater(2064):   at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:514)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:471)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:493)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.MenuInflater.parseMenu(MenuInflater.java:189)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.MenuInflater.inflate(MenuInflater.java:110)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.example.android.effectivenavigation.MainActivity.onCreateOptionsMenu(MainActivity.java:215)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:548)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:917)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:258)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.os.Handler.handleCallback(Handler.java:739)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.os.Handler.dispatchMessage(Handler.java:95)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.os.Looper.loop(Looper.java:135)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.app.ActivityThread.main(ActivityThread.java:5221)
01-19 14:17:00.222: W/MenuInflater(2064):   at java.lang.reflect.Method.invoke(Native Method)
01-19 14:17:00.222: W/MenuInflater(2064):   at java.lang.reflect.Method.invoke(Method.java:372)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-19 14:17:00.222: W/MenuInflater(2064):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-19 14:17:00.222: W/MenuInflater(2064): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-19 14:17:00.222: W/MenuInflater(2064):   at android.content.res.Resources.getValue(Resources.java:1233)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.content.res.Resources.loadXmlResourceParser(Resources.java:2598)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.content.res.Resources.getLayout(Resources.java:1049)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.support.v7.widget.SearchView.<init>(SearchView.java:282)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.support.v7.widget.SearchView.<init>(SearchView.java:268)
01-19 14:17:00.222: W/MenuInflater(2064):   at android.support.v7.widget.SearchView.<init>(SearchView.java:264)
01-19 14:17:00.222: W/MenuInflater(2064):   ... 21 more

Upvotes: 0

Views: 5059

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006624

I used the appcompat-v7

Then you should have not done this:

what I imported was import android.app.ActionBar;

That is the native action bar, not the backport. Also:

  • you need to inherit from ActionBarActivity, not FragmentActivity, and

  • you need to change android:showAsAction and android:actionViewClass to app:showAsAction and app:actionViewClass

For example, here is a sample project that uses SearchView from appcompat-v7.

The menu resource from that project is:

<?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/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom|collapseActionView"
        android:title="@string/filter">
    </item>

</menu>

Note the use of the app prefixes for actionViewClass and showAsAction.

Upvotes: 6

Related Questions