IllegalStateException: Fragment already added - java

I got this error:
java.lang.IllegalStateException: Fragment already added: MenuBottomSheetFragment{d476429 #0}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1891)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:760)
My code when open clicked MenuBottomSheetFragment:
private void openBottomSheet() {
if (mMenuBottomSheetFragment == null)
mMenuBottomSheetFragment = new MenuBottomSheetFragment();
if (!mMenuBottomSheetFragment.isShowing())
mMenuBottomSheetFragment.show(getSupportFragmentManager(), mMenuBottomSheetFragment.getTag());
}
And my MenuBottomSheetFragment have nothing special.
I really have no idea why. Most of the time, MenuBottomSheetFragment works fine. But some time, it throws this Exception then I do nothing.
So what is the problem in my case? And how can I fix it?

Replace the isShowing with this:
if(!mMenuBottomSheetFragment.isAdded()) {
mMenuBottomSheetFragment.show(getSupportFragmentManager(), mMenuBottomSheetFragment.getTag());
}

The fragment has already been added. There is no need to show it again. Just check it after the null-check and return if it's added:
if(mMenuBottomSheetFragment.isAdded()) {
return;
}

Related

java.lang.IllegalStateException: Fragment has not been attached yet Android Studio

I need to remove a QRCode scan view when pressing on a button
here is my code :
public void back() {
if (binding.vpPager.getCurrentItem() == 1) {
binding.vpPager.setCurrentItem(0);
}
else
{
scanQRcodeFragment = new ScanQRcodeFragment();
scanQRcodeFragment.removeFragmentReader();
}
}
The function to use :
public class ScanQRcodeFragment extends Fragment implements BarcodeReaderFragment.BarcodeReaderListener{
public void removeFragmentReader() {
FragmentManager supportFragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
Fragment fragmentById = supportFragmentManager.findFragmentById(R.id.container);
if (fragmentById != null) {
fragmentTransaction.remove(fragmentById);
}
fragmentTransaction.commitAllowingStateLoss();
}}
When I run the app, it crashes and I get this error :
java.lang.IllegalStateException: Fragment has not been attached yet.
at android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2383)
I found many questions with the same issue but no one resolves mine.
I need your help please.
You are trying to remove the fragment using a different object which hasn't instantiated it. scanQRcodeFragment = new ScanQRcodeFragment(); Here you are instantiating a new object which isn't added, you need a reference of the added fragment to remove it.
You're instantiating a new instance of the fragment before removing it; that doesn't make sense.
Your code should be (pseudo-code) more like:
public void back() {
if (binding.vpPager.getCurrentItem() == 1) {
binding.vpPager.setCurrentItem(0);
}
else
{
// must remove the Fragment
Fragment = f = fragmentManager.findFragmentByTag(QR_CODE_FRAGMENT_TAG);
if (f != null && f.isAttached()) {
fragmentManager
.beginTransaction()
.remove(f)
.commit();
} else {
// Fragment is not attached, nothing to do here, you can remove this code.
}
}
}
Note:
Keep in mind that commitNowAllowingStateLoss() is a dangerous operation. Depending what you do, your state (aka: the transaction) may be lost if the context goes away before the state is updated and the activity has saved its state already.
What you probably want is commitNow() because this is not a back stack operation (the downside of commitNow is that you cannot add the transaction to the backstack to pop it later), which is an expected side-effect, but not often known by some developers :)

How to disable image loading in CEF/JCEF?

Is there a switch/flag that allows to do this? I spent hours finding those but couldn't find anything that works. The other thing I'm planning to do is intercept the cefRequest by adding my own CefRequestHandler, examine the resource type and if it matches RT_IMAGE, cancel the request. Everything seems easy except the part when I have to cancel a request. How do I stop/block/cancel a cefRequest? I probably should not be doing it this way but it doesn't work anyway:
public class CefClientRequestHandler extends CefRequestHandlerAdapter {
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(CefRequest.ResourceType.RT_IMAGE)) {
cefRequest.setURL("");
}
return false;
}
// more overides
}
Any ideas?
So here's a hack that works. The trick is to change the Request Method to HEAD, and because HEAD requests aren't returned the body, images won't be part of the response.
public class CefClientRequestHandler extends CefRequestHandlerAdapter {
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(RT_IMAGE)) {
cefRequest.setMethod("HEAD");
}
return false;
}
// other overridden methods here...
}
I believe that this approach should be avoided mainly because of the following two reasons:
Changing the method from GET to HEAD does not prevent CEF from making the request to the server. The overhead of opening a connection and handling a request is still there which makes it slower than simply blocking the request.
I'm not sure if images won't be displayed if they are available from browser cache. Currently, I don't know of any methods to test this. Suggestions are welcome.
Edit 1:
Changing URL didn't work in the example I posted in the question because I was passing an empty String as the new URL. If we set the URL to some address that is not an "active" domain name (e.g. https://absolutegarbage-sdjdjfbskdfb.com), the request for that resource fails immediately:
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(CefRequest.ResourceType.RT_IMAGE)) {
cefRequest.setURL("https://yghjbnbk.com");
System.out.println("LOL!");
}
return false;
}
As you can probably guess, this still is not the best solution. Please post an answer or comment if someone has found a better solution.
Edit 2: Finally I have a clean working solution, thanks to user amaitland. We just have to pass a command line switch while setting the CefAppHandler. We can do that by overriding the method onBeforeCommandLineProcessing like this:
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
#Override
public void onBeforeCommandLineProcessing(String s, CefCommandLine cefCommandLine) {
cefCommandLine.appendSwitch("disable-image-loading");
}
#Override
public void stateHasChanged(CefApp.CefAppState state) {
if (state == CefApp.CefAppState.TERMINATED) System.exit(0);
}
});

why is drawable.java suddenly gets a lot of error?

I started my android studio and when I ran my app, I get a lot of error comming from drawable.java. For example, in the following piece of drawable.java I got:
NonNull, TRACE_TAG_RESOURCES, traceBegin, traceEnd, insets,
DENSITY_DEVICE, noncompatDensityDpi and getOpticalInsets all shows the
error "cannot resolve symbol" or "cannot resolve method".
public boolean getPadding(#NonNull Rect padding) {
padding.set(0, 0, 0, 0);
return false;
}
public Insets getOpticalInsets() {
return Insets.NONE;
}
public void getOutline(#NonNull Outline outline) {
outline.setRect(getBounds());
outline.setAlpha(0);
}
public Drawable mutate() {
return this;
}
public void clearMutated() {
// Default implementation is no-op.
}
public static Drawable createFromStream(InputStream is, String srcName) {
Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable");
try {
return createFromResourceStream(null, null, is, srcName);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
}
}
public static Drawable createFromResourceStream(Resources res, TypedValue value,
InputStream is, String srcName) {
Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable");
try {
return createFromResourceStream(res, value, is, srcName, null);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
I have never even touched drawable.java and I did not even know it ever existed. I tried cleaning up the project but nothing has changed. Is there any solution to this problem?
On Android Studio try to go to:
File -> Invalidate Caches/Restart -> Invalidate & restart.
As far as I can guess that, you've done something wrong with you layout or xml files. Please check if all the layouts are error free.
I would like to suggest a TODO list for you so that you look for all the reasons which might create this type of error.
Check all the layout files for errors.
Check all the drawables.
Check if any layout file name contains any capital letter.
Check if any image file name contains any capital letter.
If everything is okay, then do a clean and rebuild the application.

ListView unable to add and edit cell when empty

I'm trying to use a ListView as an Editor for Strings, that come out of a custom data model. I use TextFieldListCells with an appropriate StringConverter for the cells.
There is an add button next to the ListView that calls this method on action:
#FXML
private void addElement() {
WordListItem newItem = new WordListItem(-1, "");
wordListItems.add(newItem);
wordListView.setEditable(true);
wordListView.getSelectionModel().select(wordListItems.indexOf(newItem));
wordListView.edit(wordListItems.indexOf(newItem));
wordListView.setEditable(false);
}
Where wordListView is the ListView and wordListItems is the ObservableList containing the data for the wordListView.
This does work, except for when the list is empty (not null), and I couldn't quite explain why, so I inspected the Java source code for help.
Here's what I found out so far: the edit(int) call on ListView changes the ListViews internal editIndex value, which is supposed to call the EDIT_START Event. The editIndex is an ReadOnlyIntegerWrapper in which I found some weird code that I can't quite understand and I'm not sure if thats actually producing a bug or I just can't see why they did it:
#Override
protected void fireValueChangedEvent() {
super.fireValueChangedEvent();
if (readOnlyProperty != null) {
readOnlyProperty.fireValueChangedEvent();
}
}
This method is called whenever the editIndex property of ListView is changed. The problem: readOnlyProperty is null, because it's not set anywhere. The only place I could find where it got set is in the getter:
public ReadOnlyIntegerProperty getReadOnlyProperty() {
if (readOnlyProperty == null) {
readOnlyProperty = new ReadOnlyPropertyImpl();
}
return readOnlyProperty;
}
(ReadOnlyIntegerImpl is an inner private class and readOnlyProperty is it's type)
Now to my actual question: Is this a bug or am I overseeing something? Is there a reason why I can't add and edit a newly created Element in my list like that when it's empty, or is it really just this getter not being called yet?
The source code you found just is code for lazy initializing the property.
Unless new value is assigned to the property or the property itself is requested, null can be used as the property to avoid unnecessary creation of property objects. This is not an issue here.
The issue seems to be the ListView cells not being updated before edit is called. This happens during layout, so "manually" calling layout before starting the edit should work:
private void addElement() {
WordListItem newItem = new WordListItem(-1, "");
wordListItems.add(newItem);
wordListView.setEditable(true);
wordListView.layout();
wordListView.edit(wordListItems.size()-1);
wordListView.setEditable(false);
}

Constructor being called again?

I have this constructor;
public UmlDiagramEntity(ReportElement reportElement, int pageIndex, Controller controller) {
super(reportElement.getX1(), reportElement.getY1(), reportElement.getX2(), reportElement.getY2());
setLayout(null);
this.pageIndex = pageIndex;
this.controller = controller;
reportElements = reportElement.getInternalReportElements();
components = new ArrayList<AbstractEntity>();
changedComponentIndex = -1;
PageListener p = new PageListener();
this.addMouseMotionListener(p);
this.addMouseListener(p);
setPage();
}
And I have an update method in the same class;
#Override
public void update(ReportElement reportElement) {
if (changedComponentIndex == -1) {
super.update(reportElement);
} else {
reportElements = reportElement.getInternalReportElements();
if (components.size() == reportElements.size()) {
if (!isCommitted) {
if (reportElement.getType() == ReportElementType.UmlRelation) {
if (checkInvolvementAndSet(changedComponentIndex)) {
anchorEntity(changedComponentIndex);
} else {
resistChanges(changedComponentIndex);
}
return;
}
}
..................goes on
When I follow the flow from the debugger, I see that when update is called, somewhere in the method, the program goes into the constructor and executes it all over again (super, pageIndex, etc.). Why does it go to the constructor :D I didn't tell it to go there.
I can make a deeper analysis and see where it goes to the constructor if you want. By the way, changedComponentIndex is a static variable.
I would find it far more probable that you are seeing it construct two different objects. You'd have to provide more information like a stack trace; here you haven't even shown the constructor being invoked!
The behaviour you describe is pretty much impossible. Either your code is different from what you've shown or you're not debugging the code you think you're debugging. Without complete code that we can run, that's all we can say.
Are you sure that update is not called indirectly from within the constructor, which would result in a breakpoint in update getting triggered.
Try setting a breakpoint at the start of the constructor and at the end, then one in update. When you hit the first constructor breakpoint, hit 'continue' and see which breakpoint gets triggered next.
Is this multi-threaded? Is it possible that the constructor for a different instance created on another thread is being called?

Categories