I have a Options menu with sub-item menus
My app crashes when I click on the option menu, but I can't decrypt logs
I want to have a option button who open 3 option menu subitems
I have checked few tutorials but I can't see where i'm wrong
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/search_button_main_activity"
android:icon="#drawable/search_button"
android:title="#string/search_button"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/notifications_button"
android:title="#string/notifications_button"
app:showAsAction="never" />
<item
android:id="#+id/help_button"
android:orderInCategory="100"
android:title="#string/help_button"
app:showAsAction="never" />
<item
android:id="#+id/about_button"
android:orderInCategory="100"
android:title="#string/about_button"
app:showAsAction="never" />
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_button_main_activity:
this.launchSearchActivity();
return true;
case R.id.notifications_button:
Toast.makeText(this, "This will launch notification feature", Toast.LENGTH_SHORT).show();
return true;
case R.id.about_button:
Toast.makeText(this, "This will launch about feature", Toast.LENGTH_SHORT).show();
return true;
case R.id.help_button:
Toast.makeText(this, "This will launch help feature", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
here's error message :
2019-07-01 16:12:46.581 com.matt.android.mynews E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.matt.android.mynews, PID: 7624
android.view.InflateException: Binary XML file line #51: Binary XML file line #51: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #51: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:461)
at android.widget.TextView.<init>(TextView.java:771)
at android.widget.TextView.<init>(TextView.java:704)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:94)
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:161)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:174)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:208)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:177)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:806)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Logs can't tell me where the crash come from
The error you're getting is not from the menu. It's most probably from the layout you're calling in launchSearchActivity.
As I understand what you want is something like below
You can have a menu like that with the xml given below(Please change the drawables, names and ids as you want)
<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_rate" android:title="#string/action_rate"
android:icon="#drawable/ic_favorite_border_black_24dp" app:showAsAction="always|withText"/>
<item
android:icon="#drawable/ic_more_vert_black_24dp"
android:title="More"
app:showAsAction="always">
<menu>
<item android:id="#+id/action_contact_us" android:icon="#drawable/ic_email_black_24dp"
android:title="#string/action_about"
android:orderInCategory="102" app:showAsAction="never"/>
<item android:id="#+id/action_moreapps" android:icon="#drawable/ic_shop_black_24dp"
android:title="#string/action_more_apps"
android:orderInCategory="103" app:showAsAction="never"/>
<item android:id="#+id/action_exit" android:icon="#drawable/ic_close_black_24dp"
android:title="#string/action_exit"
android:orderInCategory="107" app:showAsAction="never"/>
</menu>
</item>
Related
1)This is Code declare my id at menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="home"
android:icon="#drawable/home"
android:title="HOME"/>
<item
android:id="category"
android:icon="#drawable/category"
android:title="CATEGORY"/>
<item
android:id="basket"
android:icon="#drawable/basket"
android:title="BASKET"/>
<item
android:id="me"
android:icon="#drawable/me"
android:title="ME"/>
</menu>
This is code i call my id
private BottomNavigationView.OnNavigationItemSelectedListener navigation =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
break;
case R.id.category:
Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
};
I have error when i call category id
case R.id.category:
but at case R.id.home: ,the code is right means no wrong.
Question : Who know why i cannot call my id?
can anyone give suggest for me, i take long time to fix this code. mybe i miss something like dot or comma? hahahaha
<item android:id="home"
should be
<item android:id="#+id/home"
the same applies to the others
The issue is with all the ids in your menu XML file
change it to the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
item android:id="#+id/home"
android:icon="#drawable/home"
android:title="HOME"/>
<item
item android:id="#+id/category"
android:icon="#drawable/category"
android:title="CATEGORY"/>
<item
item android:id="#+id/basket"
android:icon="#drawable/basket"
android:title="BASKET"/>
<item
item android:id="#+id/me"
android:icon="#drawable/me"
android:title="ME"/>
</menu>
I wanted to create an option menu which shows up similar to Whatsapp even if the device has its own "hardware" option button. The solution I found online worked just fine until I wanted to add another action to the Bar.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
android:showAsAction="always"
></item>
<item
android:title=""
android:id="#+id/menu_overflow"
android:icon="#mipmap/ic_more_vert_white_48dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
</item>
</menu>
And this in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
With this the app forces the addViewedMovie Action into an Overflow-Menu. Does anyone know how to prevent this?
Try this code and tell me if its work.. you should use app:showAsAction not android:showAsAction also you do not need to use submenu to show 3 dots.. just below code works fine also show 3 dots
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Movie"
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
app:showAsAction="always"/>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
Create a folder name Menu in your res folder.
menu.xml
<?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"
android:icon="#drawable/ic_launcher"
myapp:showAsAction="ifRoom" />
<item android:id="#+id/abc"
android:title="ABC" />
<item android:id="#+id/abc1"
android:title="ABC1" />
<item android:id="#+id/abc2"
android:title="ABC2" />
</menu>
In java file put this:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
case R.id.abc:
return true;
case R.id.abc1:
return true;
case R.id.abc2;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Simple.
use app instead of android as app is for design support library.
And so, in Gradle build in dependencies, you must have: compile com.android.support:design..... like that. But, I have seen that you already used it below. So,
Just use: app:showAsAction="never" instead of android:showAsAction="always" in your addViewedMovie item.
Remember: never means it will be on overflow menu item. always means is will show in action bar.
If you are using in a fragment, add below line in the onCreate
setHasOptionsMenu(true);
And two more functions
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
Inflate your views in the above method. Listen to the action in the below one.
#Override public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I am trying to display a Popup menu on the click of an action bar Icon.
The problem is that I get a forced close of the app and the Popup menu does not display. What is wrong with my code? Thank you for your help.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Toast.makeText(getApplicationContext(), "ItemSelected = " + item, Toast.LENGTH_SHORT).show();
case R.id.action_sort:
showPopup(item.getActionView());
}
return super.onOptionsItemSelected(item);
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
Toast.makeText(getApplicationContext(), "showPopup = " + popup, Toast.LENGTH_SHORT).show();
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
menu_action.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=".FileManagerActivity">
<item android:id="#+id/action_sort" android:title="#string/action_sort" android:icon="#drawable/ic_sort"
android:showAsAction="always" />
<item android:id="#+id/action_settings" android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/action_help" android:title="#string/action_help"
app:showAsAction="never" />
</menu>
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/opt1"
android:icon="#drawable/ic_sort"
android:title="Change Picture" />
<item
android:id="#+id/opt2"
android:icon="#drawable/ic_fast_forward"
android:title="Change Password" />
<item
android:id="#+id/opt3"
android:icon="#drawable/ic_fast_rewind"
android:title="Sign Out" />
</menu>
Logcat:
02-28 13:04:45.353 18838-18838/com.test.audio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.audio, PID: 18838
java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor
at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:126)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193)
at com.test.audio.TouchActivity.showPopup(TouchActivity.java:5340)
at com.test.audio.TouchActivity.onOptionsItemSelected(TouchActivity.java:5270)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1133)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:587)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:141)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
don't pass MenuItem as View cause it s not.
so it s better to cast it as view like this
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// And define click listener here.
popupMenu.show();
Source
I am using EditText in my app, I see during copy/paste operations sometimes it crashes the app. Unfortunately I couldn't replicate the error or never encountered it but there are good number of people experiencing it.
I am attaching the Log :
Fatal Exception: java.lang.IndexOutOfBoundsException: 98, -81
at android.text.PackedIntVector.deleteAt(PackedIntVector.java:218)
at android.text.DynamicLayout.reflow(DynamicLayout.java:308)
at android.text.DynamicLayout.access$000(DynamicLayout.java:41)
at android.text.DynamicLayout$ChangeWatcher.reflow(DynamicLayout.java:650)
at android.text.DynamicLayout$ChangeWatcher.onSpanChanged(DynamicLayout.java:679)
at android.text.SpannableStringBuilder.sendSpanChanged(SpannableStringBuilder.java:1019)
at android.text.SpannableStringBuilder.sendToSpanWatchers(SpannableStringBuilder.java:601)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:532)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:465)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:38)
at android.widget.TextView.paste(TextView.java:9130)
at android.widget.TextView.onTextContextMenuItem(TextView.java:8940)
at com.paprbit.dcoder.ui.widget.DcoderEditor.onTextContextMenuItem(DcoderEditor.java:881)
at android.widget.Editor$ActionPopupWindow.onClick(Editor.java:3047)
at android.view.View.performClick(View.java:4825)
at android.view.View$PerformClick.run(View.java:20144)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5537)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Here is my code :
#Override
public boolean onTextContextMenuItem(
final int id) {
boolean handled = false;
switch (id) {
case CodeNow.ID_UNDO:
handled = true;
if (canUndo()) {
undo();
}
break;
case CodeNow.ID_REDO:
handled = true;
if (canRedo()) {
redo();
}
break;
}
if (handled) {
return true;
} else {
return super.onTextContextMenuItem(id);
}
}
My xml Code Where i added the edittext
<com.paprbit.dcoder.ui.widget.DcoderEditor
android:id="#+id/et_code_content"
style="?editTextDcoderEditor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:textCursorDrawable="#drawable/cursor" />
and style for this edittext
<style name="EditTextDcoderEditor">
<item name="android:textSize">15sp</item>
<item name="android:layout_marginTop">2dp</item>
<item name="android:selectAllOnFocus">false</item>
<item name="android:imeOptions">flagNoExtractUi</item>
<item name="android:inputType">textNoSuggestions|textMultiLine</item>
<item name="android:paddingLeft">1dp</item>
<item name="android:paddingTop">1dp</item>
<item name="android:paddingBottom">1dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:isScrollContainer">true</item>
</style>
So I'm trying to create a popup menu which will popup when user presses on ImageButton.
I'm getting strange force close error on popupMenu.show():
case R.id.action_menu_edit_biljeske_uredi:
linearLayoutDodajBiljeskuBojeIOstalaSranja.setVisibility(View.VISIBLE);
imageButtonBiljeskeDodajBiljeskuBojaPozadine
= (ImageButton) findViewById(R.id.imageButtonBiljeskeDodajBiljeskuBojaPozadine);
imageButtonBiljeskeDodajBiljeskuBojaPozadine.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getApplicationContext(),
imageButtonBiljeskeDodajBiljeskuBojaPozadine);
popupMenu.getMenuInflater().inflate(R.menu.menu_popup_biljeske_boje,
popupMenu.getMenu());
popupMenu.show();
}
});
}
Does anyone know why?
Logcat
04-12 20:22:27.847 24594-24594/com.daroioradecic.studyandexamplannerbydario E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.daroioradecic.studyandexamplannerbydario, PID: 24594
java.lang.RuntimeException: Failed to resolve attribute at index 6
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6474)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6642)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:741)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:685)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.v7.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:374)
at android.support.v7.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:223)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:157)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216)
at com.daroioradecic.studyandexamplannerbydario.BiljeskeDodajBiljesku$2.onClick(BiljeskeDodajBiljesku.java:106)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="#drawable/ic_action_crvena"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeCrvena"/>
<item
android:icon="#drawable/ic_action_ljubicasta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeLjubicasta"/>
<item
android:icon="#drawable/ic_action_svijetloplava"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeSvijetloPlava"/>
<item
android:icon="#drawable/ic_action_tamnoplava"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeTamnoPlava"/>
<item
android:icon="#drawable/ic_action_zelena"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeZelena"/>
<item
android:icon="#drawable/ic_action_usranozuta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeUsranoZuta"/>
<item
android:icon="#drawable/ic_action_narancasta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeNarancasta"/>
<item
android:icon="#drawable/ic_action_siva"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeSiva"/>
</menu>
PopupMenu popupMenu = new PopupMenu(getApplicationContext(), imageButtonBiljeskeDodajBiljeskuBojaPozadine);
Replace getApplicationContext() with YourActivity.this. I think your problem will be solved.
Why getApplicationContext() is not working, visit https://possiblemobile.com/2013/06/context/ for detailed solution.