IllegalStateException asynctask - onPostExecute

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IllegalStateException asynctask - onPostExecute相关的知识,希望对你有一定的参考价值。

我发布了我的应用程序它在我的设备上运行顺畅。但是我在播放控制台上收到了一些崩溃报告。但我无法弄清楚如何处理它。我知道问题是进度对话框。我该怎么办?提前致谢。

崩溃报告:

java.lang.IllegalStateException: 
  at android.app.FragmentManagerImpl.checkStateLoss (FragmentManager.java:1328)
  at android.app.FragmentManagerImpl.enqueueAction (FragmentManager.java:1346)
  at android.app.BackStackRecord.commitInternal (BackStackRecord.java:729)
  at android.app.BackStackRecord.commit (BackStackRecord.java:705)
  at android.app.DialogFragment.dismissInternal (DialogFragment.java:292)
  at android.app.DialogFragment.dismiss (DialogFragment.java:258)
  at com.example.genesis.policeradiostream.TopFeedsFragment$Listeleme.onPostExecute (TopFeedsFragment.java:172) 
  at com.example.genesis.policeradiostream.TopFeedsFragment$Listeleme.onPostExecute (TopFeedsFragment.java:95)
  at android.os.AsyncTask.finish (AsyncTask.java:636)
  at android.os.AsyncTask.access$500 (AsyncTask.java:177)
  at android.os.AsyncTask$InternalHandler.handleMessage (AsyncTask.java:653)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:153)
  at android.app.ActivityThread.main (ActivityThread.java:5254)
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:902)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:697)

而这段片段的错误:

公共类TopFeedsFragment扩展Fragment {

public static  RadiosListAdapter adapter_3;
protected View mView;
FlipProgressDialog fpd = new FlipProgressDialog();
public TopFeedsFragment() {
}


@Override
public void onPause() {
    super.onPause();
    fpd.dismiss();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_top_feeds, container, false);
    this.mView = view;

    List<Integer> imageList = new ArrayList<Integer>();
    imageList.add(R.drawable.img_progress_dialog_red_4);
    imageList.add(R.drawable.img_progress_dialog_blue_4);

    fpd.setImageList(imageList);
    fpd.setCanceledOnTouchOutside(true);
    fpd.setBackgroundColor(Color.TRANSPARENT);
    fpd.setImageSize(600);
    fpd.setOrientation("rotationY");
    fpd.setDuration(400);


    new Runnable() {
        @Override
        public void run() {
            new Listeleme().execute();
        }
    }.run();

    return view;
}


private class Listeleme extends AsyncTask<Void,Void,Void> // LINE 95
{

    String URL="...";

    @Override
    protected  void onPreExecute()
    {
        super.onPreExecute();
        fpd.show(getActivity().getFragmentManager(),"");

    }
    @Override
    protected Void doInBackground(Void... params) {

        //...
      //Some Jsoup
        //...
        return null;
    }
    @Override
    protected void onPostExecute(Void avoid)
    {

        ListView listView_top = (ListView) mView.findViewById(R.id.listview_topfeeds);
        if (getActivity()!= null) {
            adapter_3 = new RadiosListAdapter(getActivity(), radioname, listener_number);
            listView_top.setAdapter(adapter_3);
        }


        listView_top.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                PlayRadioFragment fragment= new PlayRadioFragment();
                Bundle bundle= new Bundle();
               //...
            }
        });

        fpd.dismiss();   //LINE 172

    }
}

}

答案

您的AsyncTask无法访问ui元素。从runnable调用此行或尝试this.fpd.dismiss();

AsyncTask没有在应用程序主线程中运行,这就是您无法访问它们的原因。

另一答案

问题是您正在解雇onPostExecute中的对话框。潜在的活动可能已经消失。您必须检查活动是否仍然存在。这就是你得到IllegalStateException的原因。活动状态在消失后保存,之后您想要关闭对话框,这将无效。

以上是关于IllegalStateException asynctask - onPostExecute的主要内容,如果未能解决你的问题,请参考以下文章

IOS ASI (第三方请求)

asy CLI

IOS ASI和AFN的 区别

ASI进行POST网络请求

King

iOS中ASI和AFN的区别