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

时间:2017-11-19 23:00:13

标签: android android-fragments android-dialogfragment android-async-http

我在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. 创建的片段并调用使用AsyncHttpClient的函数。
  2. AsyncHttpClient完成返回json对象的成功结果。
  3. AsyncHttpClient成功使用json调用enterTopic。
  4. enterTopic调用regularTopic。
  5. 在这个函数(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, ""); }
    
  6. 这是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();
            }
    
        }
    
    });
    

    有人可以帮忙吗?首先,我需要在手机中重现此问题:\

2 个答案:

答案 0 :(得分:5)

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

}

答案 1 :(得分:0)

尝试在500ms延迟后调用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);