java .lang.NullPointerException: попытка вызвать виртуальный метод 'void android .widget.TextView.setText error - PullRequest
0 голосов
/ 29 мая 2020

Мое приложение построено правильно, но не работает на устройстве. При получении java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

во время выполнения ошибка:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recipeapp, PID: 2665
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.recipeapp.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:42)
        at com.example.recipeapp.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:17)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
        at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
        at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        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:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        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:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        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:726)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        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:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Код:

        public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
        private Context mContext;
        private List<Recipes> mData;

        public RecyclerViewAdapter(Context mContext, List<Recipes> 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(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,RecipeActivity.class);

                    intent.putExtra("Name",mData.get(i).getRecipeName());
                    intent.putExtra("Ingredients",mData.get(i).getRecipeIngredients());
                    intent.putExtra("MethodTitle",mData.get(i).getRecipeMethodTitle());
                    intent.putExtra("Recipe",mData.get(i).getRecipe());

                    mContext.startActivity(intent);

                }
            });

        }

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

        public 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);
            }
        }
    }



Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...