Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

android - View Binding is working for one layout, but not working for another

New to android development, I'm trying to apply View Binding as they are the recommended view referencing method by Google as of now. I have two layouts content_main.xml and content_note_list.xml. I got the first 'content_main_xml' (MainActivity) to work with View Binding. However I'm having trouble implementing view binding for second 'content_note_list.xml'.

Below is the code that keeps crashing the app within the simulator here's the image of the code block

It works when I don't use the view binding ie.

setContentView(R.layout.activity_note_list)

but as soon as I try to use the view bindings, it opens up the app and crashes momentarily

setContentView(binding.root)

I don't know what I'm doing wrong, I have followed the description provided by the official android dev site, multiple videos. Any help would be necessary.

EDIT: content_note_list.xml activity_note_list.xml

Apologize for attaching image files, the code becomes all messed up when I try to append it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems to be the name. Remembering from android documentation. If your layout is result_profile.xml

<LinearLayout ... >
    <TextView android:id="@+id/name" />
    <ImageView android:cropToPadding="true" />
    <Button android:id="@+id/button"
        android:background="@drawable/rounded_button" />
</LinearLayout>

ViewBinding will generate ResultProfileBinding.

If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID. The name of the binding class is generated by converting the name of the XML file to Pascal case and adding the word "Binding" to the end.

So, with that in mind. if your layout is activity_note_list it will generate ActivityNoteListBinding class or something like that. In your code, you are setting ContentNoteListBinding class. Try to replace ContentNoteListBinding with ActivityNoteListBinding. Also, if it does not solve the problem. Try to add the code from console log. it has more details about the error.

Update

If you want to handle View or ViewGroup which are include in your activity/fragment over <include> tag, you can access these views almost directly. You need to add an Id in this tag <include>. And then you will have access to these components: For example:

This is a activity_note_list.xml

........
<include 
    id="+@id/ly_content_list_note"
    layout="@layout/content_list_note"/>
........

And content_note_list.xml

<listView
    id="+id/listNotes"
    .......
/>

Now in your Activity class, you can access in this way:

binding.lyContentListNote.listNotes

As you can see, first access directly to the id of the container, which is ly_content_list_note and then all the view components inside that, in this case listNotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...