错误:找不到符号方法getContentResolver()

问题描述 投票:0回答:1

我想在Sqlite中添加和删除警报但出现此错误是因为我使用片段

D:\ ISLaMiC \ SoBhY \ amira \ app \ src \ main \ java \ com \ amira \ amira \ amira \ ChatCalendarProfile \ Calendar \ CalendarFragment.java错误:找不到符号方法getContentResolver()错误:找不到符号方法getContentResolver()

    Uri newUri = getContentResolver().insert(AlertEntry.CONTENT_URI, values);
      int rowsDeleted = getContentResolver().delete(AlertEntry.CONTENT_URI, null, null);

    package com.amira.amira.amira.ChatCalendarProfile.Calendar;

    import android.app.LoaderManager;
    import android.content.ContentUris;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.CursorLoader;
    import android.content.Intent;
    import android.content.Loader;
    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 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 PET_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(PET_LOADER, null, (android.support.v4.app.LoaderManager.LoaderCallbacks<Object>) 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 = getContentResolver().insert(AlertEntry.CONTENT_URI, values);
        }

        private void deleteAllAlerts() {

            int rowsDeleted = 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);
        }
    }
java android xml database
1个回答
0
投票

getContentResolver()是类android.content.Context的方法,所以称之为你肯定需要一个Context的例子(例如ActivityService)。这个案子:getActivity().getContentResolver()

© www.soinside.com 2019 - 2024. All rights reserved.