-2

I'm creating a cardview inside tablayout but i'm getting an error in adapter class.Below is the logcat,adapter class and Demo java class.I tried to find out in google as well as in stackover flow but unfortunately i unable to find the solution.Kindly help me of this error because i'm a beginner in android.

logcat

2019-04-06 22:14:37.450 32524-32524/com.example.counter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.counter, PID: 32524
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.view.LayoutInflater.from(LayoutInflater.java:229)
        at com.example.counter.MyAdapter.onCreateViewHolder(MyAdapter.java:43)
        at com.example.counter.MyAdapter.onCreateViewHolder(MyAdapter.java:28)

Adapter class

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ProductViewHolder>  { // error getting

        private Context context;
        private List<Demo> demos;



        public MyAdapter(Context context, List<Demo> demos) {
            this.context = context;
            this.demos = demos;
        }


        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {    // error getting 
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                View view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
                return new ProductViewHolder(view);

        }
 @Override
    public void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int i) {
        final Demo demo = demos.get(i);
        productViewHolder.textview1.setText(demo.getTextview1());
        productViewHolder.textview2.setText(demo.getTextview2());
        productViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tv1 = demo.getTextview1();
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("text_1", tv1);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                ((Activity)context).finish();
                Animatoo.animateSlideRight(context);
            }
        });
    }
    @Override
    public int getItemCount() {
        return demos.size();
    }
    public class ProductViewHolder extends RecyclerView.ViewHolder {
        TextView textview1, textview2;
        CardView cardView;
        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            textview1 = itemView.findViewById(R.id.textview1);
            textview2 = itemView.findViewById(R.id.textview2);
            cardView = itemView.findViewById(R.id.cardview);
        }
    }
}

Demo Class

public class Demo {
    private String textview1,textview2;

    public Demo(String textview1, String textview2) {
        this.textview1 = textview1;
        this.textview2 = textview2;
    }

    public String getTextview1() {
        return textview1;
    }

    public String getTextview2() {
        return textview2;
    }


}

Frag1.Java

public class Frag1 extends Fragment {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    Context c;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.frag_1,container,false);
        recyclerView=view.findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(c));
        ArrayList<Demo> demoList=new ArrayList<>();
        demoList.add(new Demo("A","B"));
        adapter=new MyAdapter(c,demoList);
        recyclerView.setAdapter(adapter);
        return view;
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
mohammed nabil
  • 99
  • 1
  • 6
  • 16
  • 1
    What context do use pass as a constructor parameter? – S-Sh Apr 06 '19 at 19:29
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 06 '19 at 19:39
  • @S-Sh The context is for to inflate the xml file. – mohammed nabil Apr 06 '19 at 19:43
  • @mohammednabil, I mean, how do you create `MyAdapter` instance? – S-Sh Apr 06 '19 at 19:44
  • @S-Sh I forgot to mention that i also created a java class named as Demo which contain a list,then in adapter class i create a Adapter constructor which i pass two parameter context and that java Demo class name – mohammed nabil Apr 06 '19 at 19:54
  • @mohammednabil, please, post code with appropriate `new MyAdapter(...)` statement. It's important to know what value you pass as a context – S-Sh Apr 06 '19 at 19:58
  • @S-Sh I have updated the question as well as the code,kindly check above – mohammed nabil Apr 06 '19 at 20:07
  • @mohammednabil, how do you **create** `MyAdapter` instance? Is it in activity/fragment? Post the code, please. – S-Sh Apr 06 '19 at 20:09
  • @S-Sh It is in fragment and also i have added the Fragment code at above – mohammed nabil Apr 07 '19 at 13:57

1 Answers1

-1

Context in Frag1 class is obviously null. You should pass getActivity() instead of Context in Frag1 class.

adapter=new MyAdapter(getActivity(),demoList);
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • This answer is rigth but you should explain that the context originally used is null because is not instantiated and why your context is suitable – cutiko Apr 07 '19 at 14:06