Custom SearchView crash on Release build - android

I'm getting a crash when trying to get a reference to a Custom SearchView class in onCreateOptionsMenu. Normally this works fine but when loading the release apk, it crashes. I tried adding these lines to my proguard but its still crashing:
# Android support
-keep public class com.mine.mobile.mypackage.usercontrol.SearchViewCustom
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
Here is the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
getMenuInflater().inflate(R.menu.menu_invest, menu);
final MenuItem searchItem = menu.findItem(R.id.action_invest_search);
if (searchItem != null) {
mSearchView = (SearchViewCustom) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
mPresenter = new BaseActivityPresenterImpl(this,mSearchView,mMenu,searchItem);
mPresenter.setupActionBar(NONE,false);
return super.onCreateOptionsMenu(menu);
}
Here is the log
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mine.mobile.mypackage.usercontrol.d.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
! 1 at com.mine.mobile.mypackage.activity.home.a.onCreateOptionsMenu(BaseActivity.java:94)
2 at android.app.Activity.onCreatePanelMenu(Activity.java:3232)
3 at android.support.v4.app.q.onCreatePanelMenu(FragmentActivity.java:362)
4 at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
5 at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
6 at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
7 at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
8 at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
9 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
10 at android.view.Choreographer.doCallbacks(Choreographer.java:683)
11 at android.view.Choreographer.doFrame(Choreographer.java:616)
12 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
13 at android.os.Handler.handleCallback(Handler.java:751)
14 at android.os.Handler.dispatchMessage(Handler.java:95)
15 at android.os.Looper.loop(Looper.java:154)
16 at android.app.ActivityThread.main(ActivityThread.java:6247)
17 at java.lang.reflect.Method.invoke(Native Method)
18 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
19 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
menu.xml
<?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/action_delete"
android:title="#string/delete"
android:icon="#drawable/ic_delete_white"
app:showAsAction="always" />
<item
android:id="#+id/action_done"
android:title="#string/done"
android:icon="#drawable/ic_done_white"
app:showAsAction="always" />
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh_white"
android:visible="false"
android:title="#string/refresh_menu"
app:showAsAction="always" />
<item
android:id="#+id/action_invest_search"
android:icon="#drawable/ic_search_white"
android:visible="false"
android:title="#string/action_search"
app:actionViewClass="com.mine.mobile.mypackage.usercontrol.SearchViewCustom"
app:showAsAction="always|collapseActionView" />
</menu>
This is not a duplicate of What is a NullPointerException, and how do I fix it? ...that question encompasses way too broad a category

I was on the right track but apparently my pro guard syntax was incorrect (I used google's example here:). This worked though:
-keep public class com.mine.mobile.mypackage.usercontrol.SearchViewCustom {
public *;
}

Related

onCreateOptionsMenu () method throws null pointer when support search widget is used

Activity
with #import android.support.v7.widget.SearchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
search_menu xml
<?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"
xmlns:yourapp="http://schemas.android.com/tools">
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="search"
app:showAsAction="collapseActionView|always"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
when i use support search view it throws an null pointer
java.lang.NullPointerException
at .MainActivity.onCreateOptionsMenu(MainActivity.java:42)
at android.app.Activity.onCreatePanelMenu(Activity.java:2585)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:230)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
but when i change the xml to search widget instead of support widget as follows and change the import on activity
#import android.widget.SearchView;
all works fine
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/tools">
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView" />
null poitner shows on this line if it helps
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
i have checked the available suggested solutions ..but none of them works..some soulutions are deprecated now

Material SearchView implementation error

I'm developing an android app, by now everything great, but when try to implement a Material SearchView with Google guidelines and following step by step some tutorials I can't figureout this error:
menu_main.xml
<?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_ad"
android:enabled="true"
android:icon="#android:drawable/ic_menu_search"
android:title="Buscar"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="#+id/publish_ad"
android:enabled="true"
android:icon="#android:drawable/ic_menu_send"
android:title="Publicar anuncio"
android:visible="true"
app:showAsAction="never" />
<item
android:id="#+id/favs"
android:enabled="true"
android:title="ConfiguraciĆ³n"
android:visible="true"
app:showAsAction="never" />
</menu>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_ad).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
error:
06-16 15:36:51.021 1239-1239/com.bachecubano.elbache E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bachecubano.elbache, PID: 1239
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
at com.bachecubano.elbache.MainActivity.onCreateOptionsMenu(Unknown Source)
at android.app.Activity.onCreatePanelMenu(Activity.java:2889)
at android.support.v4.b.m.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.h$b.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.q.j(Unknown Source)
at android.support.v7.app.q$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
Do you have added the following code on your manifest :
<activity
android:name=".ActivityWhereYouWantYourSearch">
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
And this on your res/xml folder (named searchable.xml) :
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name"
android:searchSettingsDescription="#string/search_description" />
I will also put a check on your onCreateOptionsMenu, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search_ad);
if (searchItem != null) {
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}
return true;
}
BTW, I don't use android:enabled and android:visible, but it's up to you to decide.
I'm not familiar with this kind of SearchView but from the error I would guess that it can't get the ComponentName of the Activity. What I would try now is to put a this before getComponentName() --> this.getComponentName() to make sure where it should get the ComponentName.
Just a guess, so please don't hate me if that's wrong ^^
You have to return true from onCreateOptionsMenu in order the inflation to take place. Therefore you get a NPE when trying to access menu before the return statement.
So remove those four lines of code to the appropriate method onPrepareOptionsMenu
I guess the issue with your code is that you are using the latest version of support library but using the old style to access the "SearchView", that's why it's returning null. Use this line of code to access your searchview through "MenuItemCompat":
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Check this blog for more details:
https://chris.banes.me/2014/10/17/appcompat-v21/
Instead of
return true;
add this line
return super.onCreateOptionsMenu(menu);

Giving error on my searchbar after extending AppCompatActivity

After extending AppCompatActivity instead of Activity, my project is giving the following error:
java.lang.NullPointerException: Attempt to invoke virtual method
'void
android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)'
on a null object reference
and I followed this link[ this][1] but it did not work for me.
Please give me some suggestions on what I am doing wrong.
This is my code for search:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
and this is menu xml
<item
android:id="#+id/search"
android:app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:app:showAsAction="collapseActionView|always"
android:title="#string/Search"/>
Try using the custom app namespace for your actionViewClass too:
app:actionViewClass="android.support.v7.widget.SearchView"/>
You have to change actionview class of searchview to support and change schema in menu. like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="Search"
android:titleCondensed="false"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="0"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Then in the code you have to get the searchview and use expand listent like this. Remember to use SearchView of support library.
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setQueryHint(mContext.getString(R.string.search_messages));
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
}
return true;
}
});

Android,OptionMenu ResourceNotFoundException; when switch to landspace

When my android app started in portrait screen, there are optionmenus in actionbar; but when switch to landspace, I'm getting resource not found exception. Exception on inflater.inflate(R.menu.main, menu);
Why I'm getting this error, Thanks
And here my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
try {
inflater.inflate(R.menu.main, menu); //Exception is here
}
catch(Exception ex){
return super.onCreateOptionsMenu(menu);
}
// Here we get the action view we defined
menuItem = menu.findItem(R.id.action_search);
View actionView = menuItem.getActionView();
// We then get the edit text view that is part of the action view
if (actionView != null) {
etActionSearch = (AutoCompleteTextView) actionView.findViewById(R.id.myActionEditText);
if (etActionSearch != null) {
// We set a listener that will be called when the return/enter key is pressed
etActionSearch.setOnEditorActionListener(this);
}
Menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/action_search"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/action_search_singlerow" />
<item
android:id="#+id/action_changeview"
android:title="Change View"
android:icon="#drawable/action_changeviewlist"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/action_refresh"
app:showAsAction="ifRoom|collapseActionView"
android:title="Refresh"/>
<item
android:id="#+id/action_sorting"
android:title="Sort"
android:icon="#drawable/action_sorting"
app:showAsAction="ifRoom|collapseActionView"
/>
Exception
android.content.res.Resources$NotFoundException: Resource ID #0x7f0e0002
at android.content.res.Resources.getValue(Resources.java:1143)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2385)
at android.content.res.Resources.getLayout(Resources.java:959)
at android.view.MenuInflater.inflate(MenuInflater.java:107)
at com.test.test.MainActivity.onCreateOptionsMenu(MainActivity.java:238)
at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:451)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:826)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:236)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5307)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
It turns out OP was using a XML layout in the menu layout file that was only defined in the layout-port folder, hence the crash. He added a layout-land version of the same layout and he fixed the issue.

MenuItemCompat.getActionView always returns null

I just implemented the v7 AppCompat support library but the MenuItemCompat.getActionView always return null in every Android version I tested (4.2.2, 2.3.4 ....)
The SearchView is displayed in action bar but it doesn't respond to touch actions and doesn't expand to show its EditText and is just like a simple icon.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (searchView != null) {
SearchViewCompat.setOnQueryTextListener(searchView, mOnQueryTextListener);
searchView.setIconifiedByDefault(false);
Log.d(TAG,"SearchView not null");
} else
Log.d(TAG, "SearchView is null");
}
return super.onCreateOptionsMenu(menu);
}
Menu.xml
<?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/action_search"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/abc_ic_search"
android:title="#string/action_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/action_bar_refresh"
app:showAsAction="ifRoom"/>
</menu>
Finally I found the solution.
Changing namespace of actionViewClass from android:actionViewClass to app:actionViewClass
Implementing android.support.v7.widget.SearchView.OnQueryTextListener interface for current activity.
Directly use setOnQueryTextListener instead of SearchViewCompat.setOnQueryTextListener
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (searchView != null) {
searchView.setOnQueryTextListener(this);
}
return super.onCreateOptionsMenu(menu);
}
In my case it was ProGuard file. You need to add this line:
-keep class android.support.v7.widget.SearchView { *; }
For me, an incorrect menu.xml namespace import caused this problem.
My original menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<item android:id="#+id/action_search"
android:title="#string/map_option_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
It looks like the xmlns:app="http://schemas.android.com/tools" was causing MenuItemCompat.getActionView() to return null. Changing this import to xmlns:app="http://schemas.android.com/apk/res-auto" fixed the problem.
New working menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/map_option_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
I think that the problem is that you use the SearchView from the Support V7 package and maybe your API level is set to.....22??.
Changing your code to the following in order to fix the problem:
menu.xml
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/actionbar_button_search"
android:title="Search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
I was with the same error, my method getActionView() was always returning null. So, I've made the following things:
<item android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
android:title="#string/search_title"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
I saw in some posts that the people are using app: or yourapp, but i've used normally android:ActionVewClass.
On my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feed, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return true;
}
And do not forget to put in the onCreate method:
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
This works very well for my activity "extending" for FragmentActivity and ActionBarActivity.
Mohsen Afshin's answer above was my starting point and I made some tweaks to get it working with my setup:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
// SearchView searchView = (SearchView) MenuItemCompat
// .getActionView(searchItem);
SearchView searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
// do something with s, the entered string
query = s;
Toast.makeText(getApplicationContext(),
"String entered is " + s, Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="#+id/action_search"
android:orderInCategory="5"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
I did this by manual set in java code:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/user_info"
android:title="#string/user_name_title"
app:actionLayout="#layout/menu_item_username"
android:showAsAction="always" />
</menu>
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/usr_name_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:contentDescription="#string/user_info_image_des"
android:padding="5dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="#string/user_name_title"
android:textStyle="bold"
android:visibility="visible" />
</LinearLayout>
Then in java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.connect_menu, menu);
// show user name on the top of menu
Log.e("menu", "Size: " + menu.size());
MenuItem item = menu.getItem(0);
item.setActionView(R.layout.menu_item_username);
View v = item.getActionView();
if (null == v) {
Log.e("NULL POINTER EX", "NULL MENU VIEW");
} else {
TextView usrNameTitle = v.findViewById(R.id.usr_name_title);
if (null != usrName && usrName.length() > 0) {
usrNameTitle.setText(usrName);
}
}
return true;
}
I had the same code, but instead of using the import android.support.v7.widget.SearchView; I was using import android.widget.SearchView; . This fixed my problem with the null value.
So just change this code in your search activity and it will work and also change the namespace in xml file.
Here's a snippet of how to handle the searchView from support library v7 :
#Override
public void onCreateOptionsMenu(final Menu menu,final MenuInflater inflater)
{
menu.clear();
getActivity().getMenuInflater().inflate(...,menu);
_searchView=(SearchView)MenuItemCompat.getActionView(_searchMenuItem);
_searchView.setQueryHint(...);
if(VERSION.SDK_INT<VERSION_CODES.HONEYCOMB)
{
final EditText searchTextView=(EditText)searchView.findViewById(R.id.search_src_text);
if(searchTextView!=null)
{
searchTextView.setScroller(new Scroller(_context));
searchTextView.setMaxLines(1);
searchTextView.setVerticalScrollBarEnabled(true);
searchTextView.setMovementMethod(new ScrollingMovementMethod());
searchTextView.setTextColor(_context.getResources().getColor(App.getResIdFromAttribute(_context,android.R.attr.textColorPrimary)));
}
}
_searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener()
{
...
});
MenuItemCompat.setActionView(_searchMenuItem,_searchView);
MenuItemCompat.setOnActionExpandListener(_searchMenuItem,new OnActionExpandListener()
{
...
});
super.onCreateOptionsMenu(menu,inflater);
}
public static int getResIdFromAttribute(final Activity activity,final int attr)
{
if(attr==0)
return 0;
final TypedValue typedvalueattr=new TypedValue();
activity.getTheme().resolveAttribute(attr,typedvalueattr,true);
return typedvalueattr.resourceId;
}
Also, if you use Proguard, add this to its configuration :
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v7.widget.SearchView { *; }
-keepattributes *Annotation*
I had a very similar issue with the difference being I was attempting to use a class that extended android.widget.ImageView
If you're using ProGuard, you need to specify to allow the methods involved in this class.
-keep public class * extends android.widget.ImageView{
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
http://proguard.sourceforge.net/manual/examples.html
This says, "Allow all needed constructors that might be called from xml and allow any custom setters it uses as well (add more as needed)"
Remove code:
public class DemoActivity extends ActionBarActivity
Replace by:
public class DemoActivity extends Activity

Categories

Resources