尝试显示对话框片段时出现 IllegalStateException

IllegalStateException when trying to show dialog fragment

我在 Google-Play 控制台上收到 IllegalStateException,我无法重现,我不明白这是什么问题。

这是来自 Google-Play 控制台的日志

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.onSuccess (ForumFragment.java:620)
    at com.loopj.android.http.TextHttpResponseHandler.onSuccess (TextHttpResponseHandler.java:118)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage (AsyncHttpResponseHandler.java:351)

根据我附上的日志,流程是这样的:

  1. 创建片段并调用使用 AsyncHttpClient 的函数。
  2. AsyncHttpClient 完成并成功结果 return json object.
  3. AsyncHttpClient 使用 json.
  4. 成功调用 enterTopic
  5. enterTopic 调用 regularTopic。
  6. 在这个函数(regularTopic)中它发生了。这是函数:

    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, ""); }
    

这是 AsyncHttpClient 请求(来自 UIThread 的 onSuccess 应该 运行):

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

    }

});

有人可以帮忙吗?首先,我需要在我的 phone 中重现这个问题:\

尝试在延迟 500 毫秒后调用 enterTopic:

//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);

此问题与此 bug 有关。 我通过重写对话框片段中的 show 方法解决了这个问题:

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

}