Caused by: java.lang.IllegalStat

2017-05-08  本文已影响85人  坚持编程_lyz
   public  void showProgressDialog() {
        Log.d(TAG, "--------------------------showProgressDialog----------------------------------");
        new Thread() {
            @Override
            public void run() {
                super.run();
                showLoadingView();//开子线程不阻塞
            }
        }.start();
    }
//利用单例锁,只要是dialog没有dismiss,那么就不再show了,只有dialog关闭了view置为null,那么就可以show,
//因为这个对象只能存在一个,这样就避免多次show对象了
    private void showLoadingView() {
        if (mGifLoadingView == null) {
            synchronized (this) {
                if (mGifLoadingView == null) {
                    mGifLoadingView = new GifLoadingView();
                    if (mGifLoadingView.isAdded()) {
                        return;
                    }
                    android.app.FragmentManager fm = getFragmentManager();
                    android.app.FragmentTransaction ft = fm.beginTransaction();
                    android.app.Fragment prev = fm.findFragmentByTag("dialog_about");
                    if (prev != null) {
                        Log.d(TAG, "------------------------------------------------------------");
                        return;
                    }
                    ft.addToBackStack(null);
                    mGifLoadingView.setCancelable(false);
                    mGifLoadingView.setImageResource(R.drawable.loading);
                    mGifLoadingView.show(getFragmentManager().beginTransaction(), "dialog_about");
                }
            }
        }
    }


    public void dismissProgressDialog() {
        Log.d(TAG, "++++++++++++++++++++++++++++++++++++隐藏了++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        if (mGifLoadingView != null) {
            mGifLoadingView.setBackGroundToNull();
        }
        android.app.FragmentManager fm = getFragmentManager();
        android.app.FragmentTransaction ft = fm.beginTransaction();
        android.app.Fragment prev = fm.findFragmentByTag("dialog_about");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        if (mGifLoadingView != null && mGifLoadingView.getDialog() != null) {
            Log.d(TAG, "MainActivity.run.");
            mGifLoadingView.onDismiss(mGifLoadingView.getDialog());
            mGifLoadingView = null;
        }
    }

经测试可以解决bug

上一篇下一篇

猜你喜欢

热点阅读