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

6
我在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$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)

根据我附上的日志,流程如下:

  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, ""); }
    
以下是AsyncHttpClient请求示例(onSuccess应该在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();
        }

    }

});

请问有人可以帮忙吗?首先我需要在我的手机上重现这个问题: \


你是在主线程调用 regularTopic 吗?你能把调用 regularTopic 的代码发出来吗? - diegoveloper
@diegoveloper 我编辑了我的帖子 - 附上了代码。 在异步任务结束后,enterTopic从onSuccess中调用。 - Eliran Tutia
你能重现这个问题吗? - diegoveloper
@diegoveloper 不,我希望不是这样。 - Eliran Tutia
2个回答

11

这个问题与这个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);
    }

}

但是使用这种解决方案(commitAllowingStateLoss),如果我们将一些东西传递给该片段,那么如果发生“非法状态异常”,我们将失去它。 - Dr.jacky

0

尝试在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);

你有没有任何线索可以在我尝试修复之前重现这个问题? - Eliran Tutia
我遇到了类似的问题,但只在某些设备上发生:/ - diegoveloper
由于:java.lang.IllegalStateException: - diegoveloper
在 Google Play 控制台中,我发现它发生在任何类型的设备和 Android 操作系统上(包括新设备)。很奇怪:\ - Eliran Tutia
你可以在你的Android模拟器上进行测试(检查设备版本和崩溃情况)以查看是否能够复现此问题吗? - diegoveloper
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接