0

Just as the title says.

I dont think my adapter and view holder are wrong (but then again its my first time using it), but upon launching the programme, my emulator crashes. Is it something to do with my layout?

(I've attached screenshots of my layouts, my code for recycler view, and the logcat message)

layout of recycler view

layout to inflate recycler view

Log Cat error message

ViewHolder

public class myPageViewHolder extends RecyclerView.ViewHolder {

TextView rName, rDesc, rDuration, rIngredients, rSteps;

public myPageViewHolder(View itemView) {
    super(itemView);
    rName = itemView.findViewById(R.id.rName);
    rDesc = itemView.findViewById(R.id.rDesc);
    rDuration = itemView.findViewById(R.id.rDuration);
    rIngredients = itemView.findViewById(R.id.rIngredients);
    rSteps = itemView.findViewById(R.id.rSteps);
}
}

Adapter

public class myPageAdapter extends RecyclerView.Adapter<myPageViewHolder> {

ArrayList<Recipe> data;

public myPageAdapter(ArrayList<Recipe> input) { data = input; }

public myPageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.mypagelayout, parent, false);
    return new myPageViewHolder(item);
}

public void onBindViewHolder(myPageViewHolder holder, int position) {
    Recipe r = data.get(position);
    holder.rName.setText(r.getName());
    holder.rDesc.setText(r.getDescription());
    holder.rDuration.setText(r.getDuration() + "mins");
    holder.rIngredients.setText(r.getIngredients()[('\n')]);
    holder.rSteps.setText(r.getSteps()[('\n')]);
}

public int getItemCount() { return data.size(); }
}

// Creates recyclerView to display recipes
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    myPageAdapter adapter = new myPageAdapter(recipeList);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

MainActivity

public class MainActivity extends AppCompatActivity {

// Create an ArrayList to store all the recipes
ArrayList<Recipe> recipeList = (ArrayList<Recipe>) getIntent().getSerializableExtra("rList");

// Receives any intent
Intent intent = getIntent();

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

    // Finds icons
    ImageView create = findViewById(R.id.createImage);

    // Create onClickListeners for create
    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gotoCreate = new Intent(MainActivity.this, createRecipe.class);
            startActivity(gotoCreate);
        }
    });

    // Creates recyclerView to display recipes
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    myPageAdapter adapter = new myPageAdapter(recipeList);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
}

}

Kelven Lim
  • 73
  • 8
  • Please add photos of your `MainActivity.java` & `activity_main.xml` and the log when the app crashes – Abdallah A. Odeh Jun 18 '22 at 05:47
  • @AbdallahA.Odeh for the xml do you want to see the code instead of the design? And why the log as well? – Kelven Lim Jun 18 '22 at 05:55
  • for the xml, the design would be better to see all views ids, the log shows what the exception is about, I believe that it's and `ids` related issue – Abdallah A. Odeh Jun 18 '22 at 06:00
  • @AbdallahA.Odeh I edited my post, it has the code for MainActivity.java, and screenshots for activity_main.xml and logcat – Kelven Lim Jun 18 '22 at 06:11
  • Move `Intent intent = getIntent();` to be inside the `onCreate()` method – Abdallah A. Odeh Jun 18 '22 at 06:13
  • @AbdallahA.Odeh still crashes – Kelven Lim Jun 18 '22 at 06:22
  • did the log message change? if so update it please – Abdallah A. Odeh Jun 18 '22 at 06:24
  • also the initialization of the arraylist `ArrayList recipeList = (ArrayList) getIntent().getSerializableExtra("rList");` must be inside the onCreate, because you can't use `getIntent` in the global scope of the activity, `ArrayList recipeList = new ArrayList();` ... and inside the onCreate: `recipeList .addAll(getIntent().getSerializableExtra("rList"));` – Abdallah A. Odeh Jun 18 '22 at 06:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245711/discussion-between-kelven-lim-and-abdallah-a-odeh). – Kelven Lim Jun 18 '22 at 06:32

0 Answers0