Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with android programming. I am getting this error and not sure how to resolve it. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.foodrecipe, PID:

I need help with android programming. I am getting this error and not sure how to resolve it.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.foodrecipe, PID: 7735 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.foodrecipe.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:41) at com.example.foodrecipe.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:16) at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065) at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107) at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114) at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303) at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587) at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665) at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134) at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851) at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:724) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:619) E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Here is my Recipe View adapter code:

package com.example.foodrecipe; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class RecyclerViewAdapter extends RecyclerView.Adapter { private final MainActivity mContext; private final List mData; public RecyclerViewAdapter(MainActivity mContext, List mData){ this.mContext = mContext; this.mData = mData; } @NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view ; LayoutInflater layoutInflater = LayoutInflater.from(mContext); view = layoutInflater.inflate(R.layout.cardview_recipe,viewGroup,false); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull MyHolder myHolder, final int i) { myHolder.recipeTitle.setText(mData.get(i).getRecipeName()); myHolder.img_recipe_thumbnail.setImageResource(mData.get(i).getThumbnail()); myHolder.cardView.setOnClickListener(v -> { Intent intent = new Intent(mContext,RecipeActivity.class); intent.putExtra("RecipeName",mData.get(i).getRecipeName()); intent.putExtra("RecipeIngredients",mData.get(i).getRecipeIngredients()); intent.putExtra("RecipeMethodTitle",mData.get(i).getRecipeMethodTitle()); intent.putExtra("Recipe",mData.get(i).getRecipe()); intent.putExtra("Thumbnail",mData.get(i).getThumbnail()); mContext.startActivity(intent); }); } @Override public int getItemCount() { return mData.size(); } public static class MyHolder extends RecyclerView.ViewHolder { TextView recipeTitle; CardView cardView; ImageView img_recipe_thumbnail; public MyHolder(@NonNull View itemView) { super(itemView); recipeTitle = (TextView)itemView.findViewById(R.id.text_recipe); img_recipe_thumbnail = (ImageView)itemView.findViewById(R.id.recipe_img_id); cardView = (CardView)itemView.findViewById(R.id.cardview_id); } } }

Main code:

package com.example.foodrecipe; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView recyclerview; RecyclerViewAdapter myAdapter; List recipes1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recipes1 = new ArrayList<>(); recipes1.add(new Recipes("Chicken Egg Rolls","1 pound ground chicken" + "2 teaspoons fresh minced garlic" + "1 teaspoon fresh minced ginger" + "salt and pepper, to taste" + "2 cups shredded cabbage" + "1 cup shredded carrots" + "1 green onion thinly sliced" + "14 egg roll wrappers" + "2 tablespoons all-purpose flour" + "1 tablespoons water" + "1 quart Vegetable oil for frying","Method", "In a large, deep skillet add the quart of oil. Heat oil over medium-low heat until it reaches 375 degrees F." + "In a separate large skillet, over medium-high heat, add ground chicken, fresh garlic and ginger. Crumble and brown chicken until thoroughly cooked. Season with salt and pepper." + "Add the shredded cabbage, carrots and sliced green onion to the chicken and stir to combine. Continue to stir-fry for 5 minutes or until mixture is heated. Turn off heat and remove skillet from burner." + "In a small bowl, mix together the flour and water to create a paste. (This paste will help seal the egg rolls before frying)" + "Take one egg roll wrapper and lay it on a flat, clean surface, with one corner pointing towards you. Place 1/4 cup of the chicken mixture into the center of the wrapper. " + "Take the bottom corner and fold up over the filling. Fold the two side corners over the center and continue to roll up. Brush a little of the flour paste on the top corner to help seal the egg roll. Repeat process until all egg rolls are wrapped." + "Carefully place 2-3 egg rolls at a time into the hot oil. Fry rolls for 2-3 minutes, using tongs to turn them so they evenly cook on all sides. They are done when they are golden brown. Remove from oil and place on a paper towel lined plate. Serve with sweet and sour sauce. ",R.drawable.eggrolls)); recipes1.add(new Recipes("Donut","1 c. whole milk" + "1/4 c. plus 1 tsp. granulated sugar, divided" + "1 packet (or 2 1/4 tsp.) active dry yeast " + "4 c. all-purpose flour, plus more if needed" + "1/2 tsp. kosher salt" + "6 tbsp. melted butter" + "2 large eggs" + "1/2 tsp. pure vanilla extract" + "Canola or vegetable oil, for frying","Method"," " + "Grease a large bowl with cooking spray and set aside. In a small, microwave-safe bowl or glass measuring cup, add milk. Microwave until lukewarm, 40 seconds. Add a teaspoon of sugar and stir to dissolve, then sprinkle over yeast and let sit until frothy, about 8 minutes. " + "Make glaze: In a large bowl, whisk together milk, powdered sugar, and vanilla until smooth. Set aside. " + "Line a large baking sheet with paper towels. In a large dutch oven over medium heat, heat 2'' oil to 350. Cook doughnuts, in batches, until deeply golden on both sides, about 1 minute per side. Holes will cook even faster! " + "Transfer doughnuts to paper towel-lined baking sheet to drain and cool slightly. Dip into glaze, then place onto a cooling rack (or eat immediately!)",R.drawable.donuts)); recipes1.add(new Recipes("Chicken Stir Fry","1 lb boneless, skinless chicken breast (cut into 1 inch cubes)" + "salt and pepper to taste" + "2 tbsp olive oil divided" + "2 cups broccoli florets" + "1/2 yellow bell pepper (cut into 1 inch pieces)" + "1/2 red bell pepper (cut into 1 inch pieces)" + "2 tsp mince ginger" + "2 garlic cloves minced","Method"," " + "Add one tablespoon of olive oil to a large skillet or wok and heat over medium high heat." + "Add chicken (in batches if necessary) and season with salt and pepper. Cook for 3 to 5 minutes or until cooked through. Remove from skillet." + "Reduce heat to medium and add remaining tablespoon of oil to the skillet." + "Add broccoli, bell pepper, and carrots and cook, stirring occasionally, just until" + "crisp tender. Add ginger and garlic and cook for an additional minute." + "Add chicken back into the skillet and stir to combine." + "Whisk stir fry sauce and pour over chicken and vegtables and stir gently to combine" + "Bring to a boil, stirring occasionally, and let boil for one minute" + "Serve with rice and/or chow mein if desired",R.drawable.stirfry)); recipes1.add(new Recipes("Pancake","1 1/4 cups milk" + "1 egg" + "3 tablespoons butter melted" + "1 1/2 cups all-purpose flour" + "3 1/2 teaspoons baking powder" + "1 teaspoon salt" + "1 tablespoon white sugar","Method", "In a large bowl, sift together the flour, baking powder, salt and sugar. Make a well in the center and pour in the milk, egg and melted butter; mix until smooth." + "Heat a lightly oiled griddle or frying pan over medium high heat. Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake. Brown on both sides and serve hot.",R.drawable.pancakes)); recipes1.add(new Recipes("Cherry Tomato & Garlic Pasta","1 tsp oil" + "8 ounces whole-wheat penne pasta" + "2 tablespoons extra-virgin olive oil" + "6 cloves garlic, peeled" + "2 cups cherry tomatoes" + "1 medium yellow squash, halved and sliced 1/2 inch thick" + " teaspoon salt" + "1 cup chopped fresh basil" + "1 cup pearl-size or mini mozzarella balls (about 4 ounces)" + "1/4 cup grated Parmesan cheese","Method", "Bring a large pot of water to a boil. Add pasta and cook according to package " + "directions. Reserve 1/4 cup cooking water, drain the pasta and cover to keep warm" + "Meanwhile, heat oil in a large nonstick skillet over medium-high heat. Add " + "garlic, reduce heat to medium and cook, stirring, until it begins to soften and " + "turn light golden, about 3 minutes. Add tomatoes, squash and salt; cook, " + "stirring occasionally, until the squash softens and the tomatoes begin to burst, " + "4 to 5 minutes. Lightly mash the garlic with the back of a spoon. Remove from " + "heat" + "finally, Add the pasta and reserved cooking water to the pan along with basil " + "and mozzarella; toss to combine. Serve topped with Parmesan.",R.drawable.pasta)); recyclerview = (RecyclerView)findViewById(R.id.recyclerView_id); myAdapter = new RecyclerViewAdapter(this,recipes1); recyclerview.setLayoutManager(new GridLayoutManager(this,1)); recyclerview.setAdapter(myAdapter); } }

Recipe code

package com.example.foodrecipe; public class Recipes { private final String RecipeName; private final String RecipeIngredients; private final String RecipeMethodTitle; private final String Recipe; private final int Thumbnail; public Recipes(String name, String recipeIngredients, String recipeMethodTitle,String recipe, int thumbnail){ RecipeName = name; RecipeIngredients = recipeIngredients; RecipeMethodTitle = recipeMethodTitle; Recipe = recipe; Thumbnail = thumbnail; } public String getRecipeName(){ return RecipeName; } public String getRecipeIngredients(){ return RecipeIngredients; } public String getRecipeMethodTitle(){ return RecipeMethodTitle; } public String getRecipe(){ return Recipe; } public int getThumbnail(){ return Thumbnail; } }

Recipe Activity code

package com.example.foodrecipe; import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.widget.TextView; public class RecipeActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recipe); TextView mRecipeName = (TextView) findViewById(R.id.text_recipe); TextView mRecipeIngredients = (TextView) findViewById(R.id.ingredients); TextView mRecipeMethodTitle = (TextView) findViewById(R.id.method); TextView mRecipe = (TextView) findViewById(R.id.recipe); Intent intent = getIntent(); String Title = intent.getExtras().getString("RecipeName"); String Ingredients = intent.getExtras().getString("RecipeIngredients"); String MethodTitle = intent.getExtras().getString("RecipeMethodTitle"); String Recipe = intent.getExtras().getString("Recipe"); mRecipeName.setText(Title); mRecipeIngredients.setText(Ingredients); mRecipeMethodTitle.setText(MethodTitle); mRecipe.setText(Recipe); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managerial Accounting

Authors: Ray H. Garrison, Eric W. Noreen, Peter C. Brewer

12th Edition

978-0073526706, 9780073526706

More Books

Students also viewed these Programming questions

Question

What general trade-off is involved in waiting-line decisions?

Answered: 1 week ago