I am writing a program using Database, LoaderManager, RecyclerAdapter and Intent. The problem is whenever a item in adapter is clicked it causes java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be null error.
package com.abhi.mynotekeeper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class NavigationSample extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
RecyclerView noteList;
NoteKeeperOpenHelper noteKeeperOpenHelper;
SQLiteDatabase db;
Cursor noteCursor;
NoteRecyclerAdapter noteRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_sample);
noteList = findViewById(R.id.note_list);
noteKeeperOpenHelper = new NoteKeeperOpenHelper(this);
noteList.setLayoutManager(new LinearLayoutManager(this));
LoaderManager.getInstance(this).initLoader(0, null, this);
displayNote();
}
private void displayNote() {
// db = noteKeeperOpenHelper.getReadableDatabase();
// noteCursor = db.query(NoteKeeperDatabaseContract.NoteInfoEntry.TABLE_NAME, null, null, null, null, null, null);
noteRecyclerAdapter = new NoteRecyclerAdapter(this, null);
noteList.setAdapter(noteRecyclerAdapter);
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
CursorLoader cursorLoader = null;
if (id == 0) {
cursorLoader = new CursorLoader(this) {
@Override
public Cursor loadInBackground() {
db = noteKeeperOpenHelper.getReadableDatabase();
Cursor cursor = db.query(NoteKeeperDatabaseContract.NoteInfoEntry.TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
};
}
Log.i("NoteActivity", "OnCreateLoader Running");
return cursorLoader;
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
noteRecyclerAdapter.swapCursor(data);
Log.i("NoteActivity", "onLoadfinish Running");
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
if (loader.getId() == 0) {
noteRecyclerAdapter.swapCursor(null);
}
Log.i("NoteActivity", "Onloaderreset Running");
}
}
NoteRecyclerAdpater Class code
package com.abhi.mynotekeeper;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class NoteRecyclerAdapter extends RecyclerView.Adapter<NoteRecyclerAdapter.ViewHolder> {
private final Context context;
List<Notes> notesList;
Cursor mCursor;
public NoteRecyclerAdapter(Context context, Cursor mCursor) {
this.context = context;
this.mCursor = mCursor;
}
@NonNull
@Override
public NoteRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.recycleview_note_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NoteRecyclerAdapter.ViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)) {
return;
}
String noteTitle = mCursor.getString(mCursor.getColumnIndex(NoteKeeperDatabaseContract.NoteInfoEntry.COLUMN_NOTE_TITLE));
String noteText = mCursor.getString(mCursor.getColumnIndex(NoteKeeperDatabaseContract.NoteInfoEntry.COLUMN_NOTE_TEXT));
int noteId = mCursor.getInt(mCursor.getColumnIndex(NoteKeeperDatabaseContract.NoteInfoEntry.COLUMN_ID));
holder.courseTitle.setText(noteTitle);
holder.courseText.setText(noteText);
holder.noteId = noteId;
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor cursor){
if(mCursor!=null){
mCursor.close();
}
mCursor = cursor;
if(cursor!= null){
notifyDataSetChanged();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView courseTitle, courseText;
public int noteId;
public ViewHolder(@NonNull View itemView) {
super(itemView);
courseTitle = itemView.findViewById(R.id.title);
courseText = itemView.findViewById(R.id.text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NoteActivity.class);
intent.putExtra("position", noteId);
context.startActivity(intent);
}
});
}
}
}
Need help to resolve the error.
I tried searching over the web but unable to find the solution of the problem.