Android,OptionMenu ResourceNotFoundException; when switch to landspace - android

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.

Related

Finding view of an icon in overflow menu

I am trying to findview of an overflow icon. After clicking and opening the overflow icon, I tried using in onoptionsitemselected:
View view = getActivity().findViewById(R.id.menu_tag); // null
View view = getActivity().findViewById(R.id.mainMenu); // not null.
<?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/mainMenu"
android:title="#string/text"
android:orderInCategory="101"
android:icon="#drawable/ic_more_vert_white_24dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_tag"
android:icon="#drawable/tag_32"
app:showAsAction="always|withText"
android:title="#string/tags"/>
<item
android:id="#+id/menu_profile"
android:icon="#drawable/user_32"
app:showAsAction="always|withText"
android:title="#string/profile"/>
<item
android:id="#+id/menu_debug"
android:icon="#drawable/insect_32"
app:showAsAction="always|withText"
android:title="#string/debug"/>
</menu>
</item>
</menu>
It is giving me null but working fine for actionbar items.
You should try to find view in onCreateOptionsMenu method like this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.optionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menufile, menu);
MenuItem item = menu.findItem(R.id.mainmenu);
return true;
}
I found a solution by setting actionview to imagebutton and then finding the view.

Android: menu displaying at screen bottom

I was trying to create a menu, and add some actions "ifRoom" using code below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/search_description" />
<item
android:id="#+id/menu_refresh"
android:title="#string/menu_refresh"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/menu_preference"
android:title="#string/menu_preferences"
android:showAsAction="never"
/>
</menu>
But what i got is the menu is displaying at the bottom...
I want it to be at top of the screen...
Any idea what happened here? Thanks!
Screenshot link: http://i.stack.imgur.com/knECg.png
Below is the MainActivity.java onCreateOptionaMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Moved from onCreate -- Retrieve the Search View and configure/enable it
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
return true;
}

android action bar doesnt appear

I am not sure why the action bar does not appear on my main menu, i already cleaned my project but it still doesn't work.
the main xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/mainmenu"
android:title="Main Menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item android:id="#+id/play_actionbar"
android:title="Play"
android:orderInCategory="2"
android:showAsAction="always"/>
<item android:id="#+id/admin_actionbar"
android:title="Admin"
android:orderInCategory="3"
android:showAsAction="always"/>
<item android:id="#+id/video_actiobar"
android:title="Video"
android:orderInCategory="4"
android:showAsAction="always"/>
the java code
#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, menu);
return true;
}
try this
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
change the code to this way and check it works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
check the link for more how it works (http://www.androidhive.info/2013/11/android-working-with-action-bar/)
In your Manifest, for your activity, make sure you declare it has Actionbar:
<activity
android:name="com.test.activities.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
You can use ActionBarsherlock or AppCompat for supporting actionbar for android versions below 3.0 .
For using ABS(actionbarsherlock)
You can extend your activity by SherlockActivity
and change your method to
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
as answered by #Amer Hadi
check here for more details ActionBar
try 100% working on 2.2 to 4.4. just add support libraries.
activity_main_actions.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" >
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
app:showAsAction="always"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
app:showAsAction="never"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_check_updates"
app:showAsAction="never"/>
code in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}

Android Action bar only showing in overflow

In my android app .. I am trying t put a search edittext with seARCH icon .. but I cant see the search icon in action bar rather the title is seeing in overflow. I am giving my code below
activity
<?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/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="Settings"/>
</menu>
MainActvity
public class MainActivity extends ActionBarActivity
{
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.wView1);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl("http://www.example.com/songs/");
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
Use Search Manager n search view in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, 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 super.onCreateOptionsMenu(menu);
}
and in your main.xml add this item for search
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
I think you must return `true` instead of
super.onCreateOptionsMenu(menu)
in `onCreateOptionsMenu(Menu menu)` method
Excuses. That was wrong.
I think this problem has something to do with the base class you are using. Your are subclassing ActionBarActivity instead of Activity. This means that you are using compatibility for previous versions. For this to work, you need to set your application's Theme to Theme.AppCompat (or a similar one), and also include the Support Library. Look here
I just realized how to fix the problem!
your menu xml file should have an starting tag like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto" >
you can replace
myApp
with any names you want, NOW the important part is when you define an action item:
<item
android:id="#+id/actionitem_switch_view"
android:title="#string/view_as"
android:showAsAction="always"
myApp:showAsAction="always"
android:icon="#drawable/ic_action_view_as_list"/>
You'd have two showAsAction tags,one for API<11 devices and one for the others,
Look closely that the two showAsAction tags are different,one is from Android namespace and the other's from the tag you defined in menu Starting point

Null pointer exception in searchview

I am trying to create list search view in shreyaloclistfragment. I am getting NullPointerException at folowing line
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
My method is
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate( R.menu.menu_search, menu);
System.out.println( "inflating menu");
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
showFilteredItems( newText );
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return;
}
Stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sears.syw.FragmentShopsTab.onCreateOptionsMenu(FragmentShopsTab.java:52)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
at
com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559 )
at
com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSh erlockNative.java:65)
at
If you are using the Appcompat library, maybe you forgot to add your own xmlns on your menu_search.xml.
As the example says:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="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"
yourapp:showAsAction="ifRoom" />
</menu>
You need to add the folowing:
<item android:id="+#id/..."
android:actionViewClass="android.widget.SearchView"
yourapp:actionViewClass="android.widget.SearchView" />
Source: https://developer.android.com/training/basics/actionbar/adding-buttons.html#XML
If you are using AppCompat library, then in your menu.xml, add
app:actionViewClass="android.support.v7.widget.SearchView"
instead of
app:actionViewClass="android.widget.SearchView"
i am using android studio
<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="com.app.myapp" >
<item
android:id="#+id/search"
android:title="Buscar"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
app:actionViewClass="android.widget.SearchView"/>
</menu>
found 100%
To reference a layout from a menu xml use the actionLayout attribute:
<menu>
<item android:id="#+id/menu_name"
android:actionLayout="#layout/your_layout"
android:showAsAction="always"/>
</menu>
Some times problem is with
setHasOptionsMenu(true);
Move it from oncreate or onviewcreate to on attached method and try it.
Check this answer
Either you are using wrong namespace or importing wrong class
https://stackoverflow.com/a/38702262/5374951

Categories

Resources