应用SwipeRefreshLayout时应用程序崩溃

时间:2014-12-18 04:47:29

标签: android android-view

我使用Google的SwipeRefreshLayout来更新我的观点。我有一个问题。如果我在工作中onRefresh()变成另一个fragment,那么第二个程序就会崩溃。

我以这种方式看到问题:我运行运行onRefresh()的方法MyAsincTask。然后我打开新的fragment。当我在新fragment MyAsincTask上终止并尝试建立数据适配器时。但我已经在其他地方了。并且应用程序崩溃了。我知道如何使用ProgressDialog - progressDialog.setCancelable(false);解决问题。在MyAsincTask没有完成工作之前,我无法移动到新的片段。但是如何在SwipeRefreshLayout中实现它?

我的代码:

swipeLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_employe_list);
        swipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);
        swipeLayout.setOnRefreshListener(this);

@Override
    public void onRefresh() {
        if (InternetResiver.isOnline(getActivity())) {
          setData();
        } else {
            AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
            alert.show();
        }
    }

我的错误:

12-18 03:46:17.655      456-456/com.managment.pavel.managmentgradle E/libEGL﹕ call to OpenGL ES API with no current context (logged once per thread)
12-18 03:46:32.085      456-456/com.managment.pavel.managmentgradle E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.nostra13.universalimageloader.core.ImageLoaderConfiguration$Builder.<init>(ImageLoaderConfiguration.java:196)
            at com.nostra13.universalimageloader.core.ImageLoaderConfiguration.createDefault(ImageLoaderConfiguration.java:127)
            at com.managment.pavel.managmentgradle.fragments.adapters.EmployeeListAdapter.<init>(EmployeeListAdapter.java:52)
            at com.managment.pavel.managmentgradle.fragments.EmployeessList$1.onPostExecute(EmployeessList.java:109)
            at com.managment.pavel.managmentgradle.fragments.EmployeessList$1.onPostExecute(EmployeessList.java:69)
            at android.os.AsyncTask.finish(AsyncTask.java:417)
            at android.os.AsyncTask.access$300(AsyncTask.java:127)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

编辑:

public class EmployeeListAdapter extends SectionedBaseAdapter {
    private ArrayList<String> sections;
    private ArrayList<List<EmployeesListBean>> employees;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private Context context;

    public EmployeeListAdapter(ArrayList<EmployeesListBean> employeesListBeans, Context context) {
        this.context = context;

        Map<String, List<EmployeesListBean>> map = new HashMap<>();
        for (EmployeesListBean employeesListBean : employeesListBeans) {
            if (!map.containsKey(employeesListBean.getDepart_name()))
                map.put(employeesListBean.getDepart_name(), new ArrayList<EmployeesListBean>());
            map.get(employeesListBean.getDepart_name()).add(employeesListBean);
        }
        sections = new ArrayList<>(map.keySet());
        employees = new ArrayList<>(map.values());
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

1 个答案:

答案 0 :(得分:0)

请参阅异常堆栈跟踪。

显示java.lang.NullPointerException at com.nostra13.universalimageloader.core.ImageLoaderConfiguration$Builder.<init>(ImageLoaderConfiguration.java:196)

所以初始化universalimageloader然后我希望它会被解决。