Question

First of all

I am using support libraries

I have a BrowseFoods activity

<activity
      android:name=".BrowseFoods"
      android:label="@string/app_name"
      android:theme="@style/MyCustomTheme" >
      <intent-filter>
          <action android:name="com.Syriatel.BROWSEFOODS" />
          <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
      <meta-data
          android:name="android.app.searchable"
          android:resource="@xml/searchable" />
      <meta-data
          android:name="android.support.PARENT_ACTIVITY"
          android:value=".MainCustomerMenu2" />
</activity>

and the code for it is :

public class BrowseFoods extends ActionBarActivity{
public boolean onPrepareOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        int currentTab = mViewPager.getCurrentItem();
        if (currentTab == 0) {
            menu.clear();
            inflater.inflate(R.menu.all_food, menu);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
                SearchManager searchManager =
                        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);

                searchView.setSearchableInfo(
                        searchManager.getSearchableInfo(getComponentName()));
                searchView.setIconifiedByDefault(false);

            }
        } else {
            menu.clear();
            inflater.inflate(R.menu.favoritefood, menu);
        }
        return super.onPrepareOptionsMenu(menu);
    }
}

In that actiivty I add two fragments as tabs , so when prepare for menu, i check which tab is currently used. Thus, i know which menu I have to use.

Please look at code, I am using all_foods menu which is:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/searchMenuItem"
          android:title="searchnNsNNN"
          android:icon="@android:drawable/ic_lock_lock"
          yourapp:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="android.support.v7.widget.SearchView" />

when I run my application, I got this exception:

02-17 11:09:38.384: E/AndroidRuntime(5064): FATAL EXCEPTION: main
02-17 11:09:38.384: E/AndroidRuntime(5064): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
02-17 11:09:38.384: E/AndroidRuntime(5064):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
02-17 11:09:38.384: E/AndroidRuntime(5064):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

Note

If i comment these codes

 // searchView.setSearchableInfo(
                 //       searchManager.getSearchableInfo(getComponentName()));
             //   searchView.setIconifiedByDefault(false);

the program works perfectly.

Please help

Was it helpful?

Solution

Change

  android:actionViewClass="android.support.v7.widget.SearchView" />

to

  yourapp:actionViewClass="android.support.v7.widget.SearchView" />

Reference: Look at adding Action Views @

http://developer.android.com/guide/topics/ui/actionbar.html

OutOfMemory generally occurs when you are trying to use large image. You run out of memory leading to OOM.

If you run into such a error make sure you scale down the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top