IllegalStateException when trying to show dialog fragment
Asked Answered
A

2

6

I get IllegalStateException on Google-Play Console which I can not reproduce and I don't understand what is the issue.

Here is the log from Google-Play Console

java.lang.RuntimeException: 
    at com.loopj.android.http.AsyncHttpResponseHandler.onUserException (AsyncHttpResponseHandler.java:304)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage (AsyncHttpResponseHandler.java:395)
    at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage (AsyncHttpResponseHandler.java:510)
    at android.os.Handler.dispatchMessage (Handler.java:102)
    at android.os.Looper.loop (Looper.java:148)
    at android.app.ActivityThread.main (ActivityThread.java:5441)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:628)

Caused by: java.lang.IllegalStateException: 
    at android.support.v4.app.FragmentManagerImpl.enqueueAction (FragmentManager.java:1515)
    at android.support.v4.app.BackStackRecord.commitInternal (BackStackRecord.java:638)
    at android.support.v4.app.BackStackRecord.commit (BackStackRecord.java:617)
    at android.support.v4.app.DialogFragment.show (DialogFragment.java:139)
    at com.example.eliran.forum.ForumFragment.regularTopic (ForumFragment.java:240)
    at com.example.eliran.forum.ForumFragment.enterTopic (ForumFragment.java:225)
    at com.example.eliran.forum.ForumFragment$13.onSuccess (ForumFragment.java:620)
    at com.loopj.android.http.TextHttpResponseHandler.onSuccess (TextHttpResponseHandler.java:118)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage (AsyncHttpResponseHandler.java:351)

According to the logs that I've attached, the flow is like this:

  1. The fragment created and calls to function which uses AsyncHttpClient.
  2. AsyncHttpClient done with success result which return json object.
  3. AsyncHttpClient success calls to enterTopic with the json.
  4. enterTopic calls to regularTopic.
  5. In this function (regularTopic) it happens. Here is the function:

    public void regularTopic(ForumTopic forumTopic, int positionInArray) {
        FragmentManager fm = getChildFragmentManager();
        ForumTopicDialogFragment dialogFragment = new 
        ForumTopicDialogFragment();
        dialogFragment.setTargetFragment(this,100);
        Bundle bundle = new Bundle();
        bundle.putInt("posInArray", positionInArray);
        bundle.putSerializable("topic", forumTopic);
        dialogFragment.setArguments(bundle);
        dialogFragment.show(fm, ""); }
    

Here is the AsyncHttpClient request (onSuccess should run from the UIThread):

AsyncHttpClient client = new AsyncHttpClient();
String url = TheFinals.HOST + "/topics/" + id;
client.get(url, new TextHttpResponseHandler() {
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        try {
            JSONArray topic = new JSONArray(responseString);
            if (topic.length() > 0) {
                JSONObject jsonObject = topic.getJSONObject(0);
                ForumTopic forumTopic = new ForumTopic(jsonObject.getInt("id"), jsonObject.getInt("userid"),
                        jsonObject.getString("content"), jsonObject.getString("title"), jsonObject.getString("firstname"),
                        jsonObject.getString("time"), jsonObject.getInt("icon"), jsonObject.getString("fbid")
                        , jsonObject.getInt("status"), jsonObject.getInt("anonymous"), jsonObject.getInt("catid"), jsonObject.getString("picture"));
                ForumFragment.this.enterTopic(forumTopic, -1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

});

Can someone help please? First I need to reproduce this issue in my phone :\

Albata answered 19/11, 2017 at 23:0 Comment(4)
Are you calling regularTopic on the main thread? could you post the code when you call to regularTopic?Pirali
@Pirali I edited my post - attached the code. enterTopic called from onSuccess after the asynctask ends.Albata
could you reproduce the issue?Pirali
@Pirali no, I wishAlbata
A
11

This issue is related to this bug. I solved it by override the show method in my dialog fragment:

@Override
public void show(FragmentManager manager, String tag) {
    try {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag).addToBackStack(null);
        ft.commitAllowingStateLoss();
    } catch (IllegalStateException e) {
        Log.e("IllegalStateException", "Exception", e);
    }

}
Albata answered 21/11, 2017 at 21:36 Comment(1)
But with this solution(commitAllowingStateLoss), if we're passing something to that fragment, we will lose it, if IllegalStateException occurs.Dorinedorion
P
0

Try calling enterTopic after 500ms delay:

//declare this as global variable on your activity
final Handler handler = new Handler();


handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    ForumFragment.this.enterTopic(forumTopic, -1);
  }
}, 500);
Pirali answered 19/11, 2017 at 23:29 Comment(9)
do you have any clue how to reproduce this issue before Ill try to fix it?Albata
I had a similar issue but only happens in some devices :/Pirali
Caused by: java.lang.IllegalStateException:Pirali
Yes, I mean the specific issue.Do you think ForumFragment.this isnt created yet?Albata
Probably, it's very weird because as I said it only happens in some devices and Android OS versionsPirali
In google-play console I see it happens in any kind of device and Android OS (also new devices). Weird :\Albata
Could you test on your Android emulator(check version of the devices crash) to see if you can reproduce the issue ?Pirali
Any kind. start from OS 5 to 8. Galaxy 5 to Pixel 2. No clue how to reproduce.Albata
check this: #11901285Pirali

© 2022 - 2024 — McMap. All rights reserved.