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

Java 错误:找不到符号方法getContentResolver(),java,android,xml,database,Java,Android,Xml,Database,我想在Sqlite中添加和删除警报,但出现此错误是因为我使用了fragment D:\ISLaMiC\SoBhY\amira\app\src\main\java\com\amira\amira\amira\ChatCalendarProfile\Calendar\CalendarFragment.java 错误:找不到符号方法getContentResolver() 错误:找不到符号方法getContentResolver() Uri newUri=getContentResolver().i

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

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

Uri newUri=getContentResolver().insert(AlertEntry.CONTENT\u Uri,value);
int rowsDeleted=getContentResolver().delete(AlertEntry.CONTENT_URI,null,null);
package com.amira.amira.amira.ChatCalendarProfile.Calendar;
导入android.app.LoaderManager;
导入android.content.ContentUris;
导入android.content.ContentValues;
导入android.content.Context;
导入android.content.CursorLoader;
导入android.content.Intent;
导入android.content.Loader;
导入android.database.Cursor;
导入android.net.Uri;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入com.amira.amira.amira.ChatCalendarProfile.Calendar.data.AlertContract;
导入com.amira.amira.amira.R;
导入静态com.amira.amira.amira.ChatCalendarProfile.Calendar.data.AlertContract.*;
公共类CalendarFragment扩展片段实现LoaderManager.LoaderCallbacks{
专用静态最终整数加载程序=0;
AlertCursorAdapter mCursorAdapter;
公共日历片段(){
}
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.activity\u calendar\u片段,容器,false);
com.github.clans.fab.FloatingActionButton addAlert=rootView.findViewById(R.id.add\u alert);
addAlert.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(getActivity(),
编辑活动类);
星触觉(意向);
}
});
ListView AlertListView=(ListView)rootView.findViewById(R.id.list);
View emptyView=rootView.findviewbyd(R.id.empty);
AlertListView.setEmptyView(emptyView);
mCursorAdapter=new-AlertCursorAdapter(getActivity(),null);
setAdapter(mCursorAdapter);
AlertListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
Intent Intent=新的Intent(getActivity(),EditorActivity.class);
Uri currentAlertUri=ContentUris.withAppendedId(AlertEntry.CONTENT\u Uri,id);
intent.setData(currentAlertUri);
星触觉(意向);
}
});
getLoaderManager().initLoader(PET_LOADER,null,(android.support.v4.app.LoaderManager.LoaderCallbacks)this);
返回rootView;
}
私有void insertAlert(){
ContentValues=新的ContentValues();
value.put(AlertEntry.COLUMN_ALERT_TITLE,“TITLE”);
value.put(AlertEntry.COLUMN_ALERT_LOCATION,“Terrier”);
value.put(AlertEntry.COLUMN\u ALERT\u DATE,“”);
value.put(AlertEntry.COLUMN\u ALERT\u sition\u TIME,“”);
value.put(AlertEntry.COLUMN\u ALERT\u rementer\u DATE,“”);
value.put(AlertEntry.COLUMN\u ALERT\u rementer\u TIME,“”);
Uri newUri=getContentResolver().insert(AlertEntry.CONTENT\u Uri,值);
}
私有void deleteAllAlerts(){
int rowsDeleted=getContentResolver().delete(AlertEntry.CONTENT_URI,null,null);
//Log.v(“CatalogActivity”,rowsDeleted+“从pet数据库中删除的行”);
}
@凌驾
公共加载器onCreateLoader(inti,Bundle){
字符串[]投影={
AlertEntry.\u ID,
AlertEntry.COLUMN\u ALERT\u标题,
AlertEntry.COLUMN\u ALERT\u位置,
AlertEntry.COLUMN\u ALERT\u DATE,
AlertEntry.COLUMN\u ALERT\u Dacement\u TIME};
返回新的游标装入器(getActivity(),//父活动上下文
AlertEntry.CONTENT\u URI,//要查询的提供程序内容URI
投影,//要包含在结果游标中的列
null,//无选择子句
null,//没有选择参数
无效);
}
@凌驾
public void onLoadFinished(加载器,光标){
mCursorAdapter.swapCursor(光标);
}
@凌驾
公共void onLoaderReset(加载器){
mCursorAdapter.swapCursor(空);
}
}
getContentResolver()
是类的方法
android.content.Context
,因此要调用它,您肯定需要一个
Context
的实例(例如
Activity
Service
)。 本例:
getActivity().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);
        }
    }