其他分享
首页 > 其他分享> > 空对象引用上的android.content.Context.getContentResolver()’

空对象引用上的android.content.Context.getContentResolver()’

作者:互联网

我似乎无法解决为什么我得到一个空指针?

这是我调用的AsyncTask来获取数据.它将它传递给JSON Parser并返回一个Objects数组.然后将其传递给我的DBHelper,它通过ContentResolver传递给我的数据库….

public class getFilms extends AsyncTask<String, Void, Void> {

public int LIMIT_FILMS = 10;
String KEY = "apikey";
String LIMIT = "limit";
private static final String URL = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?";
private static final String API_KEY = "******************";
ArrayList<HashMap<String, String>> filmArrayList = new ArrayList<HashMap<String, String>>();
Context mContext;

@Override
protected Void doInBackground(String... params) {

    Uri RottenUrl = Uri.parse(URL).buildUpon()
            .appendQueryParameter(KEY, API_KEY)
            .appendQueryParameter(LIMIT, Integer.toString(LIMIT_FILMS))
            .build();

    JSONParser jParser = new JSONParser();
    Film[] json = jParser.getJSONFromUrl(RottenUrl.toString());
    sortData(json);
    return null;
}

public void sortData(Film[] jsonlist) {
    DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);
    dbHelper.deleteAll();
    for (int i = 0; i < jsonlist.length; i++) {
        dbHelper.contentAddFilm(jsonlist[i]);
    }
}
}

这是我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

private ContentResolver myCR;

public DatabaseHelper(Context context, String name,
                      SQLiteDatabase.CursorFactory factory, int version) {
    super(context, FilmDataContract.DATABASE_NAME, factory, FilmDataContract.DATABASE_VERSION);
    myCR = context.getContentResolver();

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(FilmDataContract.FilmEntry.SQL_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(FilmDataContract.FilmEntry.DELETE_TABLE);
    onCreate(db);
}

public void addFilm(Film film) {

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(FilmDataContract.TABLE_NAME,
            null,
            values);
    db.close();
}


public Film getFilm(int id) {

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor =
            db.query(FilmDataContract.TABLE_NAME,
                    FilmDataContract.FilmEntry.COLUMNS,
                    "_id = ?",
                    new String[]{String.valueOf(id)},
                    null,
                    null,
                    null,
                    null);

    if (cursor != null)
        cursor.moveToFirst();

    Film film = new Film();
    film.setTitle(cursor.getString(1));
    film.setRating(cursor.getString(2));
    film.setRuntime(cursor.getString(3));
    film.setCritics(cursor.getString(4));
    film.setAudience(cursor.getString(5));
    film.setSynopsis(cursor.getString(6));
    film.setProfile(cursor.getString(7));

    return film;
}

public List<Film> getAllFilms() {
    List<Film> films = new LinkedList<Film>();

    String query = "SELECT  * FROM " + FilmDataContract.TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    Film film = null;
    if (cursor.moveToFirst()) {
        do {
            film = new Film();
            film.setId(Integer.parseInt(cursor.getString(0)));
            film.setTitle(cursor.getString(1));
            film.setRating(cursor.getString(2));
            film.setRuntime(cursor.getString(3));
            film.setCritics(cursor.getString(4));
            film.setAudience(cursor.getString(5));
            film.setSynopsis(cursor.getString(6));
            film.setProfile(cursor.getString(7));

            films.add(film);
        } while (cursor.moveToNext());
    }

    return films;
}

public int updateFilm(Film film) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    int i = db.update(FilmDataContract.FilmEntry.TABLE_NAME,
            values,
            "_id+ = ?",
            new String[]{String.valueOf(film.getId())});

    db.close();

    return i;
}

public int getFilmsCount() {
    String countQuery = "SELECT  * FROM " + FilmDataContract.FilmEntry.TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
}

public void deleteAll() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(FilmDataContract.FilmEntry.TABLE_NAME, null, null);
}

public boolean contentDelete(String filmName) {

    boolean result = false;
    String selection = "title = \"" + filmName + "\"";

    int rowsDeleted = myCR.delete(FilmProvider.CONTENT_URI,
            selection, null);

    if (rowsDeleted > 0)
        result = true;

    return result;
}

public Film contentFindFilm(String filmName) {
    String[] projection = FilmDataContract.FilmEntry.COLUMNS;

    String selection = "title = \"" + filmName + "\"";

    Cursor cursor = myCR.query(FilmProvider.CONTENT_URI,
            projection, selection, null,
            null);

    Film film = new Film();

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();

        film.setId(Integer.parseInt(cursor.getString(0)));
        film.setTitle(cursor.getString(1));
        film.setRating(cursor.getString(2));
        film.setRuntime(cursor.getString(3));
        film.setCritics(cursor.getString(4));
        film.setAudience(cursor.getString(5));
        film.setSynopsis(cursor.getString(6));
        film.setProfile(cursor.getString(7));

        cursor.close();
    } else {
        film = null;
    }
    return film;
}

public void contentAddFilm(Film film) {

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    myCR.insert(FilmProvider.CONTENT_URI, values);
}

这是我的堆栈跟踪……当我传递上下文时似乎正在发生.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
        at com.purewowstudio.topmovies.data.DatabaseHelper.<init>(DatabaseHelper.java:25)
        at com.purewowstudio.topmovies.util.getFilms.sortData(getFilms.java:48)
        at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:43)
        at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:16)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

在android.os.AsyncTask $SerialExecutor $1.run(AsyncTask.java:231)

解决方法:

DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);

mContext为null,因为您从不为其赋值.

标签:android,android-context,android-asynctask,android-activity
来源: https://codeday.me/bug/20191009/1875662.html