I keep getting an error every time I try to call updateList() from my adapter class. When I change updateList()
to public static void
it seems like it would work, but then I can't use this
as context and I don't know an alternative.
I'm new to android and would really appreciate the help.
MainActivity.class
public class MainActivity extends AppCompatActivity {
public void updateList(){
listitems = new ArrayList<>();
adapter = new MyAdapter(listitems, this);
recyclerView.setAdapter(adapter);}}
MyAdapter.class
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { MainActivity main = new MainActivity();
holder.setOnItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if{
AlertDialog.Builder altdial = new AlertDialog.Builder(context);
altdial.setMessage("Gelukt?").setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (listItem.getColor() == Color.BLACK) {
if (shared.loadDoneState() == 1) {
hoi[0]--;
} else if (shared.loadDoneState() == 2) {
hoi[1]--;
} else if (shared.loadDoneState() == 0) {
hoi[2]--;
}
}
shared.setDoneState(1);
shared.Recast();
dialogInterface.cancel();
Toast.makeText(context, "GOOD JOB :D", Toast.LENGTH_SHORT).show();
/*-->*/ main.updateList();
2019-08-28 19:49:05.413 30203-30203/com.reaper.newlife E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.reaper.newlife, PID: 30203
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.reaper.newlife.View.MainActivity.updateList(MainActivity.java:142)
at com.reaper.newlife.Logic.MyAdapter$1$2.onClick(MyAdapter.java:189)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)