-1

I can't not find why the app continue to crash.

Here my Main Activity

public class MainActivity extends AppCompatActivity {
List<String> catName;
List<Integer> catImages;

RecyclerView recyclerView;

String selectItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView.findViewById(R.id.recycleview);

    catName = new ArrayList<>();
    catImages = new ArrayList<>();

    catName.add("Farina");
    catName.add("Farina inverso");
    catName.add("Alcol");
    catName.add("Panna");
    catName.add("Cioccolato");

    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);

    GridLayoutManager gridLayoutManager = new GridLayoutManager
            (this,2,GridLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setHasFixedSize(true);

    categoryCardViewAdpt adapter = new categoryCardViewAdpt(this,catName,catImages);
    recyclerView.setAdapter(adapter);

Here my Adapter.

public class categoryCardViewAdpt extends   RecyclerView.Adapter<categoryCardViewAdpt.ViewHolder> {
List<String> title1;
List<Integer> image1;
LayoutInflater inflater;


public categoryCardViewAdpt (Context context, List<String> catName, List<Integer> catImage){
    this.title1 = catName;
    this.image1 = catImage;
    this.inflater = LayoutInflater.from(context);
    //this.context = context;

}

public class ViewHolder extends RecyclerView.ViewHolder{
    ImageView catImageView;
    TextView catTv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        catImageView = itemView.findViewById(R.id.category_img2);
        catTv = itemView.findViewById(R.id.category_name_2);
    }
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.card_view_layout,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull categoryCardViewAdpt.ViewHolder holder, int position) {

    holder.catTv.setText(title1.get(position));
    holder.catImageView.setImageResource(image1.get(position));
}

@Override
public int getItemCount() {
    return title1.size();
}
} 

And here my layout file

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycleview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

and here the Error i get:

java.lang.RuntimeException: Unable to start activity ComponentInfo{g.bruno.crocesandrea/g.bruno.crocesandrea.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106)

Giovanni
  • 512
  • 2
  • 6
  • 23
  • Change your recycler view id and check it again – Mahdi Zareei May 05 '22 at 06:08
  • Already done that. But nothing. – Giovanni May 05 '22 at 06:30
  • `recyclerView.findViewById(R.id.recycleview);` this is not correct. findViewById tries to find a view associated with your recycleview, which is null. `recyclerView = .findViewById(R.id.recycleview);` is how you assign a value to a variable – a_local_nobody May 05 '22 at 08:48
  • Does this answer your question? [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) – a_local_nobody May 05 '22 at 08:49

1 Answers1

-1

I found that, In your onCreate method you must change your code from

recyclerView.findViewById(R.id.recycleview);

into

recyclerView = recyclerView.findViewById(R.id.recycleview);
Mahdi Zareei
  • 1,299
  • 11
  • 18
  • I tried but it does not work. However the recyclerView is already defined on top, right? Thanks a lot Mahdi. – Giovanni May 05 '22 at 08:39
  • 1
    `recyclerView = recyclerView.findViewById` this will never work, you probably meant recyclerView = findViewById...` – a_local_nobody May 05 '22 at 08:49
  • yes you are right. It worked now! thanks a lot!! – Giovanni May 05 '22 at 09:07
  • @a_local_nobody, thanks a lot! You can post your answer, to get voted. What about if I want to implement a "OnClickListener" to the recycleView mentioned above? – Giovanni May 05 '22 at 11:39
  • no need for me to post it as an answer, this has been asked and answered a lot and, if anything, this is just a typo from your side, as you have done it correctly in other parts of your code. if you want to implement an OnClickListener for a recyclerview, there are lots of resources already available for that, here and online, you'll definitely find an answer if you do some searching, otherwise you're welcome to create a new question here and people will help you out, as long as you're not asking something which has been seen extensively before – a_local_nobody May 05 '22 at 11:41