skip to Main Content

When I normally I app run after building android studio throws this error.
at com.shayan.test.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:113)
E/AndroidRuntime: at com.shayan.test.MainActivity.onCreate(MainActivity.kt:14)

I think I forget something to do calling method or something
package com.shayan.test

package com.shayan.test

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.shayan.test.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        (binding.root)
    }
}

2

Answers


  1. your using viewbinding.. setContentView is missing. and

    binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
    

    change to

     binding = ActivityMainBinding.inflate(layoutInflater)
    

    Full code for example

    class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    
    Login or Signup to reply.
  2. Try this methods :

    //Try to add in build.gradle (Module:app)
    buildFeatures {
    viewBinding true
    }
    

    2:

    Build -> Clean Project 
    Build -> Rebuild Project
    

    If this method does not help you then It’s not about the Gradle file. It is about the XML file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search