CalendarFragment无法强制转换为android.support.v4.app.LoaderManager $ LoaderCallbacks

时间:2018-03-01 19:27:04

标签: java android xml

java.lang.ClassCastException:com.amira.amira.amira.ChatCalendarProfile.Calendar.CalendarFragment无法强制转换为android.support.v4.app.LoaderManager $ LoaderCallbacksat com.amira.amira.amira.ChatCalendarProfile.Calendar.CalendarFragment .onCreateView(CalendarFragment.java:63)

enter image description here 我想在片段

中加载警报的描述
    package com.amira.amira.amira.ChatCalendarProfile.Calendar;


import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;

import com.amira.amira.amira.ChatCalendarProfile.Calendar.data.AlertContract;
import com.amira.amira.amira.R;

import static com.amira.amira.amira.ChatCalendarProfile.Calendar.data.AlertContract.*;


public class CalendarFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final int ALERT_LOADER = 0;
    AlertCursorAdapter mCursorAdapter;

    public CalendarFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_calendar_fragment, container, false);

        com.github.clans.fab.FloatingActionButton addAlert = rootView.findViewById(R.id.add_alert);
        addAlert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), EditorActivity.class);
                startActivity(intent);
            }
        });
        ListView AlertListView = (ListView) rootView.findViewById(R.id.list);
        View emptyView = rootView.findViewById(R.id.empty);
        AlertListView.setEmptyView(emptyView);

        mCursorAdapter = new AlertCursorAdapter(getActivity(), null);
        AlertListView.setAdapter(mCursorAdapter);
        AlertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), EditorActivity.class);
                Uri currentAlertUri = ContentUris.withAppendedId(AlertEntry.CONTENT_URI, id);
                intent.setData(currentAlertUri);
                startActivity(intent);
            }
        });

        getLoaderManager().initLoader(ALERT_LOADER, null, this);


        return rootView;
    }

    private void insertAlert() {

        ContentValues values = new ContentValues();
        values.put(AlertEntry.COLUMN_ALERT_TITLE, "Title");
        values.put(AlertEntry.COLUMN_ALERT_LOCATION, "Terrier");
        values.put(AlertEntry.COLUMN_ALERT_OCCASION_DATE, " ");
        values.put(AlertEntry.COLUMN_ALERT_OCCASION_TIME, " ");
        values.put(AlertEntry.COLUMN_ALERT_REMINDER_DATE, " ");
        values.put(AlertEntry.COLUMN_ALERT_REMINDER_TIME, " ");
        Uri newUri = getActivity().getContentResolver().insert(AlertEntry.CONTENT_URI, values);
    }

    private void deleteAllAlerts() {
        int rowsDeleted = getActivity().getContentResolver().delete(AlertEntry.CONTENT_URI, null, null);
        //Log.v("CatalogActivity", rowsDeleted + " rows deleted from pet database");
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        String[] projection = {
                AlertEntry._ID,
                AlertEntry.COLUMN_ALERT_TITLE,
                AlertEntry.COLUMN_ALERT_LOCATION,
                AlertEntry.COLUMN_ALERT_OCCASION_DATE,
                AlertEntry.COLUMN_ALERT_OCCASION_TIME};
        return new CursorLoader(getActivity(),   // Parent activity context
                AlertEntry.CONTENT_URI,   // Provider content URI to query
                projection,             // Columns to include in the resulting Cursor
                null,                   // No selection clause
                null,                   // No selection arguments
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mCursorAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mCursorAdapter.swapCursor(null);
    }
}

1 个答案:

答案 0 :(得分:0)

请在片段中的onActivityCreated(..)方法中初始化加载器..它有望解决您的问题

首先override方法,然后在那里初始化