Android: Emulator won't open menu in app - android

I'm having a rather odd issue where the menu for an activity works totally fine on real device but not on the emulator.
I tried launching one of my older projects in the emulator that I remember the menus working in, and it also failed. I suppose some sort of update has caused this?
The code is simple...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem prefs = menu.add("Preferences");
prefs.setIcon(R.drawable.gear_01);
return true;
}
LogCat displays series of exceptions ultimately caused by:
E/AndroidRuntime(6714): Caused by: java.io.FileNotFoundException: res/drawable-hdpi/ic_menu_more.png
This actually happens not only in my applications but on the home screen to.
Any ideas for solutions?

Its hard to find error in your code.
So try this.Create one Folder in res named menu.Put this xml inside this folder
Make Sure you have icon in your drawable names ic_new_game,ic_help
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Override this method as you did
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}

Related

onCreateOptionsMenu crashes only on Oreo

EDIT: Problem solved after upgrading Build Tools Version from 23 to 27.
I have the following code snippet:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
}
Recently I have got some errors on Crashlytics with line menu.findItem(...):
Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
at pl.application.ProductViewFragment.onCreateOptionsMenu(SourceFile:434)
at android.support.v4.app.Fragment.performCreateOptionsMenu(SourceFile:2186)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(SourceFile:2250)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(SourceFile:328)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(SourceFile:363)
The problem is not deterministic and shows only on Android Oreo (by Fabric - 100% of crashes were on Android 8.0, different devices). I have never had problems with this line before.
Were there any important changes in Android 8.0 able to cause NPE there? I've tried reproduce it on my Xiaomi Mi A1, but with no effects. Or maybe there is a workaround?
Thanks!
// Edit: added xml menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/breadcrumbsMenuItem"
android:icon="#drawable/ic_breadcrumbs"
android:title="#string/breadcrumbs"
res:showAsAction="always" />
<item
android:id="#+id/shareMenuItem"
android:icon="#drawable/ic_menu_share"
android:title="#string/share"
res:actionLayout="#layout/layout_share_button"
res:showAsAction="always" />
</menu>
Just try to add something like this, to see if it will be still null or not
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
}
}, 2000);
If this fixes the problem, then the reason for him to be null is that is still hasn't been created and you area all rdy trying to access to it
I did have a problem when I was trying to change the color of an icon of my Menu acording to some info that it get's when the program starts, and that "work arround" fix the problem to me
I'm not sure where you get res:... but i think it should be app:...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/breadcrumbsMenuItem"
android:icon="#drawable/ic_breadcrumbs"
android:title="#string/breadcrumbs"
app:showAsAction="always" />
<item
android:id="#+id/shareMenuItem"
android:icon="#drawable/ic_menu_share"
android:title="#string/share"
app:actionLayout="#layout/layout_share_button"
app:showAsAction="always" />
</menu>

How do I get my ActionBar Items to show as icons, and not in the overflow menu?

First off, I'm running the latest Lollipop - Minimum and targeted SDK are both 22
minSdkVersion 22
targetSdkVersion 22
in the gradle build. I've changed/re changed these to 22, as well as rebuilt/invalidated caches/cleaned. I'm extending Activity in my main:
public class login_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
And here are my menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="ifRoom"/>
</menu>
When I run my code, it keeps showing the items in the ActionBar overflow menu. How do I fix this? Thanks!
Try replacing "ifroom" with "always" for showAsAction like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="always"/>
</menu>
I actually spent nearly five hours trying to figure this out, but I actually figured it out just now going through my gradle files. Here's how you fix it:
Go to your Gradle Scripts folder
Open the build.gradle file with the SDK options in it
Remove the line under dependencies (Mine's this one, not sure how they vary):
compile 'com.android.support:appcompat-v7:22.2.0' (REMOVE THIS)
Go back to your menu file and REMOVE the namespace definition for the namespace you put before showAsAction
xmlns:app="http://schemas.android.com/apk/res-auto" (REMOVE THIS)
Go change the showAsAction line from something:showAsAction to android:showAsAction
Rebuild & compile. It should work! I wish changing the SDK cleaned up after itself, but hey, what can you do.

Creating an Options Menu not working for me

Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and then inflating the menu in the activity as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.
My activity extends FragmentActivity, in case that matters.
The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.
I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.

Getting a SearchView with MenuItemCompat (Android)

I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble.
(http://developer.android.com/guide/topics/ui/actionbar.html).
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) I have a problem with the class MenuItemCompat. It says:
The method getActionView(MenuItem) is undefined for the type MenuItemCompat
I can only use for this class the following methods:
setShowAsAction(item, actionEnum)
setActionView(item, view)
Here it is the code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) There is a problem with this:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
I don't understand why it is used but if google says it, it must be appropriate.
Error message:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.pruebahttp3'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.pruebahttp3'
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search_menu"
android:orderInCategory="100"
android:title="#string/search"
android:icon="#drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
Thank you very much!
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/abc_searchview_description_search" />
`
For the 1st:Fixing the second one will fix this :)
For the 2nd:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
Change myapp to you application namespace com.xxx.xxx
Try to copy the lib files directly from yourFolder\sdk\extras\android\support\v7\appcompat\libs
I have a similar problem,but It occurs to me when i directly copy the JAR library file rather than following the android support library procedure. Try the opposite it might work for you.
Kinda weird if you ask me.

How to create custom option menu in android

I am creating an option menu. it has three option HOME, EMAIL, VISIT. All three options coming at the same row. i have to show HOME option above and rest two below to Home Option(according to attached Screen Shot).
Any Ideas??
Thanks.
Here is my XML file.
<menu android:id="#+id/new_game"
android:icon="#drawable/red_no_bg"
android:title="Home" />
<item android:id="#+id/email"
android:icon="#drawable/red_no_bg"
android:title="Email" />
<item android:id="#+id/help"
android:icon="#drawable/red_no_bg"
android:title="Visit Microsite" />
</menu>
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.overview_menu, menu);
return true;
}
Using the above code i am getting the all option in Same line.
You can Create in two ways
1.Statically.
2.Dynamically.
Am explaining the easiest method of adding menus.
Step 1: Create a new Android Application.
Step 2: After onCreate() method write onCreateOptionMenu();
Step 3: In onCreateOptionMenu() Write these lines
menu.add(11, 21, 1, "Gowtham").setIcon(R.drawable.ic_launcher);
Here "Gowtham" is title of your menu item You can Home&Email use instead of "Gowtham".
Ans use Any images Instead of ic.launcher

Categories

Resources