onCreateOptionsMenu crashes only on Oreo - android

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>

Related

android-Popup menu onitemclick error

I'm trying direct each popup menu item to the another activity by using intent,but i get an error shown below:
This is my java code of popup menu:
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
Intent i = new Intent(Cihazlar.this,MainActivity.class);
startActivity(i);
return true;
case R.id.two:
Intent i2 = new Intent(Cihazlar.this,Kampanya.class);
startActivity(i2);
return true;
}
return true;
}
});
And this is popup menu xml:
<menu xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/tools">
<item
android:id="#+id/one"
androclass:title="Senin Dünyan" />
<item
android:id="#+id/two"
androclass:title="Destek" />
<item
android:id="#+id/three"
androclass:title="Sıkça Sorulan Sorular" />
So why am i getting this error? Any help will be appreciated.
the problem is not at run time. It's compile time error. So if error is like resource not found then error is while importing R package or error in menu.xml.
But menu.xml looks proper, so I think, main problem is with R.java import. Check you have not imported com.android.R. It should be your <package_name>.R.
And even if this is ok, then your resources are not compiled completely. There might be some error at some other resource file (.xml file)
The very last thing one can try is to rebuild your app and then check out
EDIT
I think error is in menu.xml file itself. Try below code, unless you have made some customize stuff-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/one"
android:title="Senin Dünyan" />
<item
android:id="#+id/two"
androclass:title="Destek" />
<item
android:id="#+id/three"
android:title="Sıkça Sorulan Sorular" />
bro have u declared it in Manifest plz post your manifest code

"refresh_tasks cannot be resolved or is not a field"

I basically copy & pasted the code from right above and replaced the necessary tidbits, yet I still get this error. I'm trying to make a "Refresh" item in my menu.
XML code:
<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/refresh_tasks"
android:title="#string/refresh"/>
</menu>
HomeActivity.java code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh_tasks: //line where I get error
loadTasksFromAPI(TASKS_URL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I've searched across the internet, but I haven't managed to fix this. I assume something is wrong with my XML, but I don't see what it could be.
So, apparently my R.java stopped updating for some reason. My code was correct, but it wouldn't update. Editing the AndroidManifest.xml file and then saving seems to force R.java into updating. It's as simple as adding a space and then deleting the space from your AndroidManifest.xml file before you save (though, you could leave the space if you wish... It shouldn't cause an error).
Make sure there are no errors in any of your resources
. This would cause AAPT to stop generating a new R.class for you
Clean and rebuild the project
EDIT: Try to add this at the beginning of the xml:
<?xml version="1.0" encoding="utf-8"?>

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.

Animation-list on Menu Item Icon

I want to have an Menu item in the ActionBar that visualizes a boolean value. If this boolean value is true, the icon should start animating a series of 2 images in a loop. I did the following:
I added an item in the menu.xml file:
<item
android:id='#+id/myAnimation'
android:icon='#drawable/pic1'
android:showAsAction='ifRoom'>
</item>
I created an animation-list:
<?xml version='1.0' encoding='utf-8'?>
<animation-list xmlns:android='http://schemas.android.com/apk/res/android'
android:oneshot='false' >
<item
android:drawable='drawable/pic1'
android:duration='100' />
<item
android:drawable='drawable/pic2'
android:duration='100' />
</animation-list>
In the method onCreateOptionsMenu I assign this menu item to a variable
myMenuItem = menu.findItem(R.id.myAnimation);
Now, I was hoping to add an animation which would get executed whenever I tell the menu item to animate. But I wasn't able to come even close.
is this even possible?
How can I do that?
Thanks! Any help is appreciated!
Edit:
The App should support API from 8
I did ((AnimationDrawable) item.getIcon()).start(); in onCreateOptionsMenu(Menu menu)
This did not work. Then I thought, maybe it should be done on the UI thread, and then this worked:
someView.postDelayed(new Runnable() {
#Override
public void run() {
((AnimationDrawable) item.getIcon()).start();
}
}, 1000);
I was looking for the same thing and just found your question unanswered, so here is the solution, from the Android dev website:
((AnimationDrawable)myMenuItem).start();

Android: Emulator won't open menu in app

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;
}

Categories

Resources