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

时间:2018-03-01 15:40:21

标签: java android xml database

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

  

d:\伊斯兰\ SoBhY \阿米拉\应用\ SRC \主\的java \ COM \阿米拉\阿米拉\阿米拉\ ChatCalendarProfile \日历\ CalendarFragment.java    错误:找不到符号方法getContentResolver()    错误:找不到符号方法getContentResolver()

enter image description here

    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);
        }
    }

1 个答案:

答案 0 :(得分:0)

getContentResolver()是类android.content.Context的方法,因此要调用它,您肯定需要Context的实例(例如ActivityService)。 这种情况:getActivity().getContentResolver()