1.MovieDetailsFragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (mMovie != null) {
inflater.inflate(R.menu.menu_detail, menu);
Log.d(LOG_TAG, "detail Menu created");
final MenuItem action_fav = menu.findItem(R.id.action_fav);
MenuItem action_share = menu.findItem(R.id.action_share);
//set proper icon on toolbar for favored movies
new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... params) {
return Utility.isFavored(getActivity(), mMovie.getMovie_id());
}
@Override
protected void onPostExecute(Integer isFavored) {
action_fav.setIcon(isFavored == 1 ?
R.drawable.ic_favorite_black_24dp :
R.drawable.ic_favorite_border_black_24dp);
}
}.execute();
}
}
2.Utility
public class Utility {
//takes movie_id and tells whether or not that movie is favored
public static int isFavored(Context context, int id) {
Cursor cursor = context.getContentResolver().query(
MovieContract.FavEntry.CONTENT_URI,
null, // projection
MovieContract.FavEntry.COLUMN_MOVIE_ID + " = ?", // selection
new String[] { Integer.toString(id) }, // selectionArgs
null // sort order
);
int numRows = cursor.getCount();
cursor.close();
return numRows;
}
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference at com.example.deepankmehta.popularmovies2.Utility.isFavored(Utility.java:20) at com.example.deepankmehta.popularmovies2.MovieDetailsFragment$1.doInBackground(MovieDetailsFragment.java:108) at com.example.deepankmehta.popularmovies2.MovieDetailsFragment$1.doInBackground(MovieDetailsFragment.java:105)