应用程序在后台崩溃

时间:2016-10-20 08:32:29

标签: android android-viewpager

我从API获取数据并将其显示在ViewPager中,并使ViewPager自动使用计时器进行滑动,当我将应用程序置于后台并再次打开时,它已崩溃

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{317597f7 V.ED.... R......D 0,0-160,160} not attached to window manager
                                                                     at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
                                                                     at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
                                                                     at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
                                                                     at android.app.Dialog.dismissDialog(Dialog.java:341)
                                                                     at android.app.Dialog.dismiss(Dialog.java:324)
                                                                     at com.example.app.fragments.LatestNewsFragment$3.onSuccess(LatestNewsFragment.java:178)
                                                                     at com.example.app.fragments.LatestNewsFragment$3.onSuccess(LatestNewsFragment.java:126)
                                                                     at com.example.app.tools.connection.ServerTool$4.onSuccess(ServerTool.java:202)
                                                                     at com.example.app.tools.connection.ServerTool$4.onSuccess(ServerTool.java:197)
                                                                     at com.example.app.tools.connection.RetrofitTool$APICallBack.onResponse(RetrofitTool.java:71)
                                                                     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)

这是崩溃的代码段

 private void getLastNews() {
    if (dialogLoading == null) {
        dialogLoading = new LoadingDialog().showDialog(getActivity());
    }
    ServerTool.getLastNews(getActivity(), new ServerTool.APICallBack<List<NewsListModel>>() {
        @Override
        public void onSuccess(List<NewsListModel> response) {

            Log.d("response list", response.size() + "");
            newsList = response;
            NewsPagerAdapter helpAdapter = new NewsPagerAdapter(getActivity(), newsList);
            viewPagerImage.setAdapter(helpAdapter);
            timer = new Timer();
            viewPagerImage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if (position < newsList.size()) {
                        if (position < currentPage) {
                            currentPage--;
                        } else {
                            currentPage++;
                        }
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (getActivity() == null)
                        return;
                    if (isAdded())
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (currentPage < newsList.size()) {
                                    viewPagerImage.setCurrentItem(currentPage++, true);
                                } else {
                                    currentPage = 0;
                                    viewPagerImage.setCurrentItem(currentPage, true);
                                }
                            }

                        });
                }
            }, 300, 2500);
            if (dialogLoading.isShowing()) {
                dialogLoading.dismiss();
            }
        }

        @Override
        public void onFailed(int statusCode, ResponseBody responseBody) {

            Log.d("onFailed", responseBody + "");
            if (dialogLoading.isShowing()) {
                dialogLoading.dismiss();
            }
        }


    });


}

2 个答案:

答案 0 :(得分:1)

Android操作系统会在隐藏后立即销毁活动。所以需要在你的活动中破坏onDestroy中的dismissDialog。

@Override
protected void onDestroy() {
 dialogLoading.dismiss();
super.onDestroy();
}

答案 1 :(得分:0)

在解除对话框之前添加(dialogLoading!=null && dialogLoading.isShowing())