Home > database >  how i can fix my android studio app crashing
how i can fix my android studio app crashing

Time:01-27

when i want run my app i get 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 somthing 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)
    }
}

CodePudding user response:

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)

CodePudding user response:

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.

  • Related