装载机在完成时崩溃

时间:2018-01-23 23:29:05

标签: android

我无法找到错误,为什么我的装载机会在完成时崩溃......

public class SubjectsFragment extends ListFragment implements
        LoaderManager.LoaderCallbacks<SubjectsFragment.Data> {

    private static final int SUBJECT_LOADER_ID = 0;

    private DetailsAdapter mAdapter = new DetailsAdapter();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            actionBar.setTitle(R.string.title_activity_subjects);
        }

        Drawable icEmpty = VectorDrawable.getDrawable(getContext(),
                R.drawable.ic_labels_color_48dp);
        getBinder().emptyView.setIcEmpty(icEmpty);
        getBinder().emptyView.setMsgEmpty(getString(R.string.msg_no_subjects));
        getBinder().prvContent.setAdapter(mAdapter);

        LoaderManager lm = getLoaderManager();
        lm.initLoader(SUBJECT_LOADER_ID, null, this).forceLoad();

        return view;
    }

    @Override
    public Loader<Data> onCreateLoader(int id, Bundle args) {

        if (SUBJECT_LOADER_ID == id) {
            return new SubjectLoader(getContext());
        }

        return null;
    }

    @Override
    public void onLoadFinished(Loader<Data> loader, Data data) {

        if (data != null) {
            mAdapter.clear();
            Resources res = getResources();

            for (int i = 0; i < data.subjects.size(); ++i) {
                Table.Subject subject = data.subjects.get(i);
                int count = data.links[i];
                String links = res.getQuantityString(R.plurals.timetable_links_count, count, count);
                DetailsAdapter.DetailsRecord record = new DetailsAdapter.DetailsRecord()
                        .setId(subject.getId())
                        .setTitle(subject.getTitle())
                        .setSubtitle(links)
                        .setColor(ColorUtils.generateColor());
                mAdapter.add(record);
            }

            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoaderReset(Loader<Data> loader) {

    }

    private static class SubjectLoader extends DatabaseLoader<Data> {

        SubjectLoader(Context context) {
            super(context);
        }

        @Override
        public Data loadInBackground() {
            Data data = new Data();
            DatabaseHandler db = getDatabase();
            db.openReadable();
            data.subjects = db.getSubjects();
            data.links = new int[data.subjects.size()];

            for (int i = 0; i < data.subjects.size(); ++i) {
                Table.Subject subject = data.subjects.get(i);
                List<Table.Timetable> timetables = db.getTimetablesBySubject(subject.getId());
                data.links[i] = timetables.size();
            }

            db.close();

            return data;
        }
    }

    static class Data {
        List<Table.Subject> subjects;
        int[] links;
    }
}

LogCat的输出:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.contedevel.timetable.modal.SubjectsFragment$Data
at com.contedevel.timetable.modal.SubjectsFragment.onLoadFinished(SubjectsFragment.java:29)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:476)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:444)
at android.support.v4.content.Loader.deliverResult(Loader.java:128)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:257)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:82)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:487)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:504)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

错误在哪里?

1 个答案:

答案 0 :(得分:0)

您是否尝试过更改

LoaderManager.LoaderCallbacks<SubjectsFragment.Data> 

LoaderManager.LoaderCallbacks<ArrayList<SubjectsFragment.Data>> 

并更新

public void onLoadFinished(Loader<Data> loader, Data data) 

public void onLoadFinished(Loader<Data> loader, ArrayList<Data> data)