'cannot find symbol variable' in android data binding include layout

前端 未结 1 2093
面向向阳花
面向向阳花 2021-02-14 02:18

layout_content.xml


    

        
1条回答
  •  无人共我
    2021-02-14 02:41

    The layout activity_main.xml:

    
        
    
            
    
                
    
            
        
    
    

    generates ActivityMainBinding.java. In your MainActivity.java, you use the generated field for content in the setSupportActionBar argument:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        setSupportActionBar(binding.content.toolbar);
    }
    

    Normally a layout will generate public final fields for each of the Views with android:ids and Binding subclasses for each of the includes with IDs. In this case, the data binding system did not detect that the included content @layout/layout_content was a binding layout and thus didn't capture the Binding class for the include.

    When a variable is bound to an include, the data binding system will use that to determine that the included layout is a binding layout. So, if your layout had this instead:

    
    

    You'd have gotten a content field with the type LayoutContentBinding. This does assume that someVar is declared in both activity_main.xml and layout_content.xml.

    The error in Android Studio was pointing to the correct location, but it was difficult to understand. In the future, you can look for the generated binding class in your app/build directory. This may help you figure out what the error means.

    I've filed a bug to fix the error -- we should be generating a public final field for the include with the ID.

    0 讨论(0)
提交回复
热议问题