Otto
Otto

Reputation: 19

Android Studio: "cannot resolve symbol 'recyclerView'" but the RecyclerView symbol does work

I'm having this wierd problem where i am trying to add an itemDecorator for my recyclerview, but for some reason Android Studio wont aknoledge the "recyclerView" in the code "recyclerView.addItemDecorator(itemDecoration);" it should be noted that RecyclerView with capital letter "R" does work. I have also importet the RecyclerView class in the begining of my activity, and i have implemented the RecyclerView in my gradle build. Maybe i have done something wrong, or misunderstood something:

Here is a bit of my code:

@Override
public void onClick(View view) {


                    RecyclerView rvCalculations = (RecyclerView) findViewById(R.id.rvCalculations);
                    //calculations = Calculation.createCalculationsList(50);
                    calculations.add(0, new Calculation("  " + mNameEditText.getText()));
                    CalculationsAdapter adapter = new CalculationsAdapter(calculations);
                    rvCalculations.setAdapter(adapter);
                    rvCalculations.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                    adapter.notifyItemInserted(0);
                    rvCalculations.scrollToPosition(0);
                    RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL);
                    recyclerView.addItemDecorator(itemDecoration);



                    if (!mNameEditText.getText().toString().isEmpty())
                        Toast.makeText(MainActivity.this, R.string.ErrorMessageNameCalculation,Toast.LENGTH_SHORT);
                     else
                        Toast.makeText(MainActivity.this, R.string.SuccesMessageNameCalculation,Toast.LENGTH_SHORT);


                }
            });

Everything else works, exept from this line "recyclerView.addItemDecorator(itemDecoration);"

Thank you in advance.

Upvotes: 0

Views: 99

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

recyclerView.addItemDecorator(itemDecoration);
^^^^^^^^^^^^               ^^

should be

rvCalculations.addItemDecoration(itemDecoration);
^^^^^^^^^^^^^^               ^^^

When invoking a method on an instance you name the instance, not the class. Also, the method name is addItemDecoration, not addItemDecorator.

Upvotes: 1

Related Questions