last day (15 days later) » 

14:58
0
Q: Cannot find symbol method getCurrency() in Android View Binding

Real NoobHere is my activity_main.xml file: <LinearLayout ..> <TextView android:id="@+id/currency" .... /> </LinearLayout> Here is my other code: public View getView(int position, View convertView, ViewGroup parent) { binding = ActivityMainBinding.inflate(getLayoutInflater(), parent, false); con...

Is your layout nested/included in a parent XML file? And have you enabled view binding in the gradle file? Also, have you tried to build the app? Sometimes the IDE is slow to recognize/find the auto-generated content...
@TylerV I have added the contents of my build.gradle file to the question. I get the error when I run the app by pressing Shift + F10. Thanks. :)
Is the activity_main XML nested/multiple files? (e.g. something like activity_main.xml and content_main.xml)
@Seelenvirtuose I am voting to re-open since this question is about why the binding auto-generated method is missing, not "what does the error mean?"
@TylerV there is only one file called activity_main.xml. Thanks for voting to re-open the question. :)
Where is this getView located? Normally you would inflate an activity binding in onCreate. Is this inside an adapter or something?
14:58
@TylerV It goes like this: MainActivity > onCreate > anonymous ArrayAdapter > getView. :)
@TylerV Yes, it is inside an Adapter. Is that a problem?
Ok, I think this is the problem. Does "currency" live in a row in a ListView/RecyclerView, or in the main activity? You should have a separate XML file you use for the list rows and inflate that inside getView, not the main activity one. You should definitely not be inflating the main activity layout inside an adapter.
@TylerV The Java code is all inside the MainActivity class and there is only one layout file activity_main. Should I create a separate layout file where I place the layout, update the refrences and everything will work just fine?
If the code is all inside MainActivity, why are you making an ArrayAdapter? Please post sufficient details in your question (a minimal but complete activity code, adapter, + XML) to help us understand what you are asking - using comments like this is not effective.
@TylerV I am very new to Android so learning by doing things. I am just having a hard time finding tutorials that will explain everything. I pieced together my own code by reading multiple tutorials so I made many mistakes. :)
@Seelenvirtuose - it is declared in the question as an ActivityMainBinding - have a read about view binding
@RealNoob - that's ok, it's a learning process. You should try to explain better what you are trying to do in the question - show the full code, not just the part you think has the current problem. What are you trying to display?
14:58
@TylerV I know I provided very little details and I am impressed by the fact that you were still able to pinpoint the probable cause of the error. :)
Hi @TylerV Just wanted to say again that you are a genius and very patient with beginners like me. :D
We were all beginners once
How long will this chat stay active?
It might take some time for me to piece together the full code. :)
I will try to be as quick as possible
No idea - i've had ones stay open for multiple days
no rush
also, this is useful reading for learning to avoid question asking pitfalls (meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - easy to get hung up on the specific problem you are seeing and forget to also tell the big picture
So you can do something else while I put together the code right?
Yeah, I just leave this open and check back periodically
Also, have a look at the activity example here that uses view binding. stackoverflow.com/questions/57117338/…
15:04
Thanks for the confirmation. I will place the whole code here shortly. Just cleaning up a few things. :)
That is where you should be inflating the activity binding.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ResultProfileBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
}
Here is my `activity_main.xml`file:

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Country"
android:id="@+id/name" />
<TextView
android:layout_width="wrap_content"
Ok, so you definitely don't need an ArrayAdapter in there
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);

binding.getCurrency().setText("test");
}
Here is my MainActivity.java file:

package com.example.simpleapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;
import com.example.simpleapp.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
So you want to make a grid of "Country" and "Currency" entries?
15:10
@TylerV could you please recommend some up-to-date tutorials on Android from beginner to advanced topics?

A lot of what I found myself is either outdated or very fragmented.
I want to display country name and the currency below it
in a grid :)
Are you using java because you have to? Or like it?
(vs Kotlin)
You will find a lot of the newer tutorials/examples will be in Kotlin
Although Java is still fully supported
You can use a RecyclerView to display a grid of data like you are looking to do
I prefer Kotlin but some of the tutorials I was reading were in Java so I decided to go ahead with Java.

Is Kotlin the preferred way of doing Android Development or is Java still more popular?
I would say Kotlin is the preferred way these days
I learned some basics of Android about 8 years back and just started looking at it again
I migrated all my apps from Java to Kotlin - it is a lot "friendlier" of a language IMO
and Kotlin and Java are interoperable, so you can still use Java libraries etc
but for your main question, try making a RecyclerView like at the guide I posted - a RecyclerView can be either a linear layout or a grid layout
your main activity XML would just have a single <RecyclerView that takes up the whole screen
and then you would make a separate xml file to define the layout to use in each "grid cell"
15:16
Thanks, for the documentation link Tyler. I was also looking at the View Binding documentation from the official website and combined what I learned there with other tutorials.
@TylerV So, thw same thing happens with ListView as well?
Here is another tutorial raywenderlich.com/…
What do you mean with ListView?
ListView is an older capability that doesn't have as many features as RecyclerView (e.g. can't show a grid) but otherwise is similar - you would make an XML to show for each row and an adapter to control how those rows data gets set
I made a simple test app for learning some things that does this - I can share some example code here
The activity

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCatsBinding.inflate(layoutInflater)
setContentView(binding.root)

val adapter = CatFactAdapter()
binding.factList.adapter = adapter
}
The adapter (in a separate file to keep things clean)

class CatFactAdapter : ListAdapter<CatFact, CatFactAdapter.ViewHolder>(DIFF_CALLBACK) {

class ViewHolder(val binding: CatFactRowBinding) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = CatFactRowBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
this is all Kotlin
so in the Activity onCreate, I inflate the Activity binding, create the adapter, and set it on the RecyclerView

In the Adapter in `onCreateViewHolder` I inflate the binding for the row layout (a separate XML)
```
test - does code formatting work in chat?
```
apparently no...
Hehe, I am sorry for the confusion. :)

The tutorial you linked looks good. I will read all the links and your own code that you posted here and try to build my app. I will let you know if there are any errors or if I succeed. :)

You have been very helpful. It is truly rare to find people like you. :)
I will be able to open this chat later if I close the tab right?
yeah, the link should be in the comments on your original question
Thanks @TylerV I hope you have a great day ahead. :)
 
2 hours later…
16:57
Hi @TylerV :)

I know I am using the wrong approach for making the app work but I can't get this thing off my mind. Could you please help me figure out what exactly is causing the failure here?

Is there something wrong with the code or is there no way to make the app work with the approach that I am taking?

Here is m
*Here is my code for different files:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/grid_view"
android:layout_width="match_parent"
tools:listitem="@layout/country_list"
android:layout_height="match_parent" />
country_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Country"
android:id="@+id/name" />
<TextView
android:layout_width="wrap_content"
MainActivity.java

package com.example.easyapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;
import com.example.easyapp.databinding.CountryListBinding;

public class MainActivity extends AppCompatActivity {

private CountryListBinding binding;
Thanks. :)
The problem happens with this line: binding.getName().setText(countries[position].name);
17:31
What happens? Cannot resolve "getName" or some other error?
17:46
This is the exact error that I get :)

error: cannot find symbol
binding.getName().setText(countries[position].name);
^
symbol: method getName()
location: variable binding of type CountryListBinding
I just pulled your code, turns out the documentation for view binding with java is out of date - you now use binding.name.setText not binding.getName().setText.
your code is pretty close, I'll post the update here
but once I do, it would be a good idea for you to edit your question to reflect what you pasted above - the fact that getName() was removed is not in the official docs
the docs here (developer.android.com/topic/libraries/view-binding) show getName() like you were doing. Typing "binding." in Android Studio will show you a context hint for what options are available
Here is a working onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

ArrayAdapter<Country> cheeseAdapter =
new ArrayAdapter<Country>(this, 0, countries) {

@Override
public View getView(int position,
View convertView,
ViewGroup parent) {

CountryListBinding rowBinding;

if(convertView == null) {
rowBinding = CountryListBinding.inflate(getLayoutInflater(), parent, false);
note that there are two bindings - you have to inflate the main activity binding at the top level, and the row binding inside the adapter
"turns out the documentation for view binding with java is out of date" this disappointed me very much. :(

A lot of information related to Android in general seems to be either very fragmented or out-of-date.
Thank you very much Tyler :)
I wasted a lot of my time due to outdated documentation
I suggest you update your question to reflect that (you can even remove the adapter and just show that "getGridView()" doesn't work)
I'll add an answer, and hopefully it will help someone else avoid the same trouble you ran into
If you want I can help edit the question.
Thanks Tyler I have a couple of questions. :)
I can't find any getGridView() in the code you posted and there is no such method in my code as well
yeah, like getName it is now just "binding.gridView"
18:00
I understand now. We will be modifying the code and the question in such a way that it highlights that the documentation is outdated. :)
I have one more question. Did we use two bindings because there are two layout files?
Yes, the main activity plus the layout for each grid cell
I made an update to the question - feel free to edit it further if you feel I've mis-represented your question.
Thanks Tyler the edit looks good. :)

Its concise and explains the issue properly. I have just one more question.
Creating the listview layout in the activity_main.xml file would be a bad practice but would it have worked if I just made changes to the binding part?
I'm not sure I follow
you mean putting your "country" and "currency" TextViews in the main activity XML?
Yes. :)
No, that would not work. For things like ListView, GridView, RecyclerView, you need a separate layout for it to use for its items
The activity itself also needs a layout to display the List/Grid/Recycler view in
so you're always going to need separate layouts when making this list-type containers
18:08
Thank you very much Tyler. :) I learned a lot of new things today.
If the list is really simple (e.g. just show some text) android has some pre-made layouts you can use (don't remember the exact code but it's something like android.R.layout.single_item_list)
but for anything more complicated you'd have to make it yourself
I know I asked this earlier and you recommended reading the official Android documentation. However, it might turn out to be outdated in some cases like it did here.

While the official documentation is good at explaining things. It doesn't seem to have any example of combining concepts to see how they fit together. Is there any YouTube channel or something else thatyu could recommend for some practical up-to-date tutorials aswell?
Could you please post an answer on the question so that I can accept and upvote it?
Those are just as likely to be out of date
I can post an answer once it is re-opened
can't add answers on closed questions
How many votes does it need to get re-opened?
If you want example code, the android sample apps (developer.android.com/samples) are a bit more up-to-date
3 votes I think
e.g. here's their sample RecyclerView demo app

https://github.com/android/views-widgets-samples/tree/main/RecyclerView
but even that is like 3 years since it was updated...
reading answers in the SO android channel is sometimes helpful, I've learned new things that way
18:14
Hehe. I voted for re-opening it as well. Maybe someone else will too.
Thank you very much for the link to sample apps Tyler.
Yeah, just have to wait at this point
May I ask where you are from? :)
18:30
USA (Seattle)
18:50
Yes, that's what my guess would have been as well.

Someone nice like you helped me figure out an issue with my code in chat a few years back. He was also from the US.

People in the US are a lot more helpful than what the news articles might suggest. :D
FWIW, the text in the docs is actually correct, it's just the example code that is wrong

"The generated binding class is called ResultProfileBinding. This class has two fields: a TextView called name and a Button called button."
Thanks Tyler. :)

I read that too. I just assumed it was creating getter and setter methods automatically for those fields after I looked at the sample code.
Yeah, I would have assumed that too
19:06
Bye Tyler. :)

It was a real pleasure discussing this with you.

last day (15 days later) »