EditText Crash while using paste : Fatal Exception: java.lang.IndexOutOfBoundsException: 98, -81 - java

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>

Related

Can't find Menu ID

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>

onClick button causing crash

I've cloned this git depo it's an application that allows you to capture or choose images and save them in a pdf document.
It work fine, but when I tried to integrate in my app module (new project) and clicked on the button to start the activity it crash. I haven't changed anything the Logcat isn't helping either...
Any ideas ?
Note : I already added the activity in my manifest.
Edit : I added the style and layout below
styles
<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
activity_pdf
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nabeeltech.capturedoc.imgtopdf.PdfActivity">
<Button
android:id="#+id/btnPdf"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:text="Generate Document"
android:layout_height="wrap_content" />
</LinearLayout>
PdfActivity
Button btn_Pdf = findViewById(R.id.btnPdf);
activity = this;
filename=new Date().toString();
btn_Pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getImages();
}
});
}
private void getImages() {
Config config = new Config();
config.setToolbarTitleRes(R.string.str_bar_tool);
ImagePickerActivity.setConfig(config);
Intent intent = new Intent(this, ImagePickerActivity.class);
startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);
}
#Override
protected void onActivityResult(int requestCode, int resuleCode, Intent intent) {
super.onActivityResult(requestCode, resuleCode, intent);
if (requestCode == INTENT_REQUEST_GET_IMAGES && resuleCode == Activity.RESULT_OK) {
ArrayList<Uri> image_uris = intent.getParcelableArrayListExtra(ImagePickerActivity.EXTRA_IMAGE_URIS);
if(image_uris.size()!=0){
ArrayList<String> tempUris = new ArrayList<>();
for (Uri uri : image_uris) {
tempUris.add(uri.getPath());
}
CallBackCreatePdf callBackCreatePdf = new CallBackCreatePdf() {
#Override
public void OnCallBackCreatePdf(String tPath) {
if(tPath!=""){
Toast.makeText(getBaseContext(),"Pdf Created", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(),"Error creating PDF", Toast.LENGTH_LONG).show();
}
}
};
MaterialDialog.Builder builder = new MaterialDialog.Builder(activity)
.title("Creating PDF")
.content("just a moment ...")
.cancelable(false)
.progress(true, 0);
MaterialDialog dialog = builder.build();
AsynCreatePdf asynCreatePdf = new AsynCreatePdf(activity,callBackCreatePdf,dialog,tempUris,"1",("pdf"+(new Date()).getSeconds()));
asynCreatePdf.execute();
}
Log.d(LOG_ACTIVITY, "onActivityResult");
}
}
Here is the stack trace
2020-02-05 16:44:36.884 17311-17311/com.nabeeltech.capturedoc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nabeeltech.capturedoc, PID: 17311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nabeeltech.capturedoc/com.gun0912.tedpicker.ImagePickerActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:421)
at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:150)
at com.gun0912.tedpicker.ImagePickerActivity.initView(ImagePickerActivity.java:95)
at com.gun0912.tedpicker.ImagePickerActivity.onCreate(ImagePickerActivity.java:82)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:421)
Need Another theme. At first goto manifest
<activity android:name=".ImagePickerActivity"
android:theme="#style/CustomTheme"
android:screenOrientation="portrait"
/>
Then go to res/values/styles.xml section. Create Theme. Your Theme will be
<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Fixed. Thanks to #intellij-amiya
I missed another style. I was using
<activity android:name="com.nabeeltech.capturedoc.imgtopdf.PdfActivity"
android:label="#string/title_activity_pdf"
android:theme="#style/MyAppTheme"/>
<activity android:name="com.gun0912.tedpicker.ImagePickerActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
android:screenOrientation="portrait"
/>
So I replaced ImagePickerActivity with
android:theme="#style/AppTheme_picker"
Now it works.

How to fix OptionsMenu with subItem InflateException

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>

Option menu not working with theme ThemeOverlay.AppCompat.Dark.ActionBar

I have created an application which has a RecyclerView and each item has an option menu, which will show the popup to share. Everything is working fine but when I try to click on the option menu it crashes. I am getting a weird error. I google it and it seems like there is something wrong with my theme. I tried many solutions available on the internet but none of them working for me.
Here is my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#color/about_us_link_text_color</item>
</style>
This is my Adapter class
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popUp = new PopupMenu(context,holder.buttonViewOption);
popUp.inflate(R.menu.option_menu);
popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,news.get(position).getUrl());
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
context.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return false;
}
});
popUp.show();
}
});
option_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/share"
android:title="Share" />
<item
android:id="#+id/save"
android:title="Save" />
</menu>
The error I am getting is :
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffff4081 a=3 r=0x7f0c001a}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:446)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385)
at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93) 
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160) 
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153) 
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187) 
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290) 
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175) 
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141) 
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233) 
at com.kotlin.whatshappening.activity.adapter.NewsAdapter$2.onClick(NewsAdapter.java:142) 
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Can someone tell me what I am doing wrong?
Instead of:
PopupMenu popUp = new PopupMenu(context, holder.buttonViewOption);
Perform:
PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);
Try this
popUp.getMenuInflater().inflate(R.menu.option_menu, popUp.getMenu());
I hope this helps you.
First create a method in you adapter class like the following.
private void showPopup(View view, String url) {
PopupMenu popup = new PopupMenu(activityContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.option_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,url);
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
activityContext.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return true;
}
});
popup.show();
}
And in your onBindViewHolder method call the above function,
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopup(view, news.get(holder.getAdapterPosition()).getUrl());
}
});

Popup menu crash at show();

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.

Categories

Resources