0

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)
levio
  • 11
  • 3
  • Where in your code, `recyclerView ` is being initialized? – marcinj Aug 28 '19 at 18:16
  • `MainActivity main = new MainActivity();` – You cannot instantiate `Activity` classes yourself, and have them work correctly. The system must handle `Activity` instantiation; otherwise they're not properly initialized, and never go through their lifecycle methods, like `onCreate()`. Furthermore, a _new_ instance wouldn't be the same as one already on-screen, yeah? If you want to trigger code in your `Activity` from the `Adapter`, create and implement an `interface` that you can use to call back to the `Activity`, like is demonstrated in [this post](https://stackoverflow.com/q/40584424). – Mike M. Aug 28 '19 at 18:52

0 Answers0