I have got Activity extends AppCompatActivity. I want to set searchview query after change screen orientation. I got NPE error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference
If i use setQuery in Fragment it works right. In Activity it does not work.
How can I change searchview query from activity ?
MyActivity.java
#Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bankmvp_menu, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchManager = (SearchManager) this.getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
getObservableSearchViewText();
return true;
}
#Override public void setQueryToSearch(String querystring) {
searchView.setQuery(querystring, true); //NPE error
}
bankmvp_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyActivity">
<item
android:id="#+id/action_setmonth"
android:orderInCategory="100"
android:title="#string/action_setmonth"
app:showAsAction="never" />
<item
android:id="#+id/action_setaccount"
android:orderInCategory="100"
android:title="#string/action_setaccount"
app:showAsAction="never" />
<item
android:id="#+id/action_nosaveddoc"
android:orderInCategory="100"
android:title="#string/action_nosaveddoc"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
To set query string from the Presenter to Searchview
Change menu.xml
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:iconifiedByDefault="true" //important
app:showAsAction="always" //important
Change activity, from Presenter set only string.
#Override public void setQueryToSearch(String querystringx) {
if( querystringx.equals("")){
}else {
querystring = querystringx;
}
}
Change activity setQuery in onResume of activity
if( querystring.equals("")){
}else {
searchView.setIconified(false);
searchView.setQuery(querystring, false);
menuItem.setVisible(true);
}
Screen after change orientation
Related
Faced an error in the onCreateOptionsMenu method during menu inflate.
I do not understand why he refers to this field - it seems that I brought everything and did it correctly under androidx.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Поиск последний новостей ...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (query.length() > 2) {
onLoadingSwipeRefresh(query);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchMenuItem.getIcon().setVisible(false, false);
return true;
}
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.MainActivity">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="always"
app:actionProviderClass="androidx.appcompat.widget.SearchView"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never" />
<item
android:id="#+id/action_weather"
android:orderInCategory="100"
android:title="Weather"
android:icon="#drawable/ic_weather"
app:showAsAction="always" />
</menu>
How can this be fixed? Thank you in advance!
P.S. A log with a description of the error is listed below:
Process: com.sv.newsapp, PID: 16248
java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to androidx.core.view.ActionProvider
at androidx.appcompat.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:425)
at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:179)
at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:129)
at com.sv.newsapp.activities.MainActivity.onCreateOptionsMenu(MainActivity.java:150)
You need to use app:actionViewClass, not app:actionProviderClass, as SearchView is a CollapsibleActionView, not an ActionProvider.
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
/>
I am really stuck with this.
I want to use a CAB in a fragment, so I'm using
actionMode = ((MainActivity)getActivity()).startActionMode(actionModeCallbacks);
to call startActionMode
I've been changing themes and adding and testing several lines of code (together and separated) to xml styles like:
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
Relevant information:
I have my toolbar defined inside and <include/>in my main activity.
Toolbar is included just before the frame of MainActivity, between that frame and the DrawerLayout, so I can use the toolbar in the fragments.
I have tested the CAB inside Main Activity, but it also gets above
the Toolbar.
I also tried a dirty workoaround toggling Toolbar visibility when CAB
is created/destroyed, but it also doesn't work!
I'm using android.view.ActionMode instead of
android.support.v7.view.ActionMode because I can't call
startActionMode if I use Support Library (?).
My toolbar (*android.support.v7.widget.Toolbar):
toolbar = findViewById(R.id.toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
toolbar.setTitle(R.string.addWordLabel_Act);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
XML (toolbar)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FAFAFA"
android:theme="#style/AppTheme.NoActionBar"
app:collapseIcon="#drawable/ic_arrow_back_white_24dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/addWordLabel_Act"
app:titleTextColor="#color/colorAccent"
/>
</android.support.constraint.ConstraintLayout>
ActionMode.Callback
private ActionMode actionMode;
private ActionMode.Callback actionModeCallbacks = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_mode_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share_actionMode:
shareWord();
mode.finish();
return true;
case R.id.deleteWords_actionMode:
deleteWords();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
XML (CAB)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Share"
android:icon="#drawable/ic_share_white_24dp"
android:id="#+id/share_actionMode"/>
<item android:id="#+id/deleteWords_actionMode"
android:title="Delete"
android:icon="#drawable/ic_round_delete_outline_white_24px"/>
</menu>
And styles...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/lightAccent</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="android:fontFamily">#font/sans_serif_roboto</item>
<item name="alertDialogTheme">#style/AlertDialogStyle</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
<style name="AppTheme.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
</style>
I don't know what else I can do.
Solved
I completely forgot to check Manifest, and that was the problem!
The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I completely forgot to check Manifest, and that was the problem! The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I want a options menu after the drawer toggle button like this app. And I want options menu in left, write both sides (don't wanna compromise any). Can anyone tell me how to do that?
-: My tool bar :-
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
-: My options menu :-
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.option_menu, menu);
}
-: In 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/0"
android:icon="#drawable/ic_0"
android:title=""
app:showAsAction="always"
android:orderInCategory="0"/>
<item
android:icon="#drawable/ic_1"
android:orderInCategory="1"
android:title=""
app:showAsAction="always">
<menu>
<item
android:id="#+id/2"
android:icon="#drawable/ic_2"
android:orderInCategory="2"
android:title="2" />
<item
android:id="#+id/3"
android:icon="#drawable/ic_3"
android:orderInCategory="3"
android:title="3" />
<item
android:id="#+id/4"
android:icon="#drawable/ic_4"
android:orderInCategory="4"
android:title="4" />
<item
android:id="#+id/5"
android:icon="#drawable/ic_5"
android:orderInCategory="5"
android:title="5" />
<item
android:id="#+id/6"
android:icon="#drawable/ic_6"
android:orderInCategory="6"
android:title="6" />
</menu>
</item>
</menu>
I try to do a tuto about Menu.
My JAVA code :
public class MenuActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.myfirstmenu, menu);
return true;
}
}
My XML "R.layout.activity_menu" code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- nothing because I just want display a menu -->
</LinearLayout>
My XML "R.menu.myfirstmenu" code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Item 1"></item>
<item android:id="#+id/item2" android:titleCondensed="Item 2" android:title="Item 2 mais avec un nom assez long quand même">
<menu>
<item android:id="#+id/item3" android:title="Item 2.1" android:checkable="true"/>
<item android:id="#+id/item4" android:title="Item 2.2"/>
</menu>
</item>
<item android:id="#+id/item5" android:title="Item 3" android:checkable="true"/>
<item android:id="#+id/item6" android:title="Item 4">
<group android:id="#+id/group1" android:checkableBehavior="all">
<item android:id="#+id/item7" android:title="Item 4.1"></item>
<item android:id="#+id/item8" android:title="Item 4.2"></item>
</group>
</item>
<group android:id="#+id/group2" android:enabled="false">
<item android:id="#+id/item9" android:title="Item 5.1"></item>
<item android:id="#+id/item10" android:title="Item 5.2"></item>
</group>
</menu>
My problem is that when I run my app, nothing appear !
What I must do in order to display the three vertical dots and the app name ?
Thank for your answers ;)
Max
I tried to add two action buttons on the action bar, for visibility one is defined as:
android:showAsAction="ifRoom"
another one is defined as:
android:showAsAction="never"
The problem is I can see the ic_action_search icon but I could not see the ic_action_overflow icon. This is the main_activity_actions.xml in menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyFirstApp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
MyFirstApp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:icon="#drawable/ic_action_overflow"
android:title="#string/action_settings"
MyFirstApp:showAsAction="never" />
</menu>
and this is the Java code that includes the buttons:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So whats wrong with my code?
cheers
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
it is work