0

I am actually trying to develop an android application, to do so I added all the requirements and imports for that, but I am unable to fix this issue. After applying clean project and rebuild project I get this error every time. Image: Unresolved reference: budgetLeft

Whenever I try to add binding to these three id(s), I don't understand why is there this question mark with these two variables and not with the third one? Image: binding error

activity_main.xml

...
...
...
<TextView
            android:id="@+id/total_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/total_balance_in_digits"
            android:textSize="32sp"
            android:textStyle="bold"/>
...
...
...
<TextView
                    android:id="@+id/budget_left"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/budget_in_digits"
                    android:textColor="@color/green"
                    android:textSize="26sp"
                    android:textStyle="bold" />
...
...
...
<TextView
                    android:id="@+id/total_expense"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/expense_in_digits"
                    android:textColor="@color/red"
                    android:textSize="26sp"
                    android:textStyle="bold" />
...
...
...

MainActivity.kt

package com.myapp.budgettracker

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.oldschoolbastard.budgettracker.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var transactions: ArrayList<Transaction>
    private lateinit var transactionAdapter: TransactionAdapter
    private lateinit var linearLayoutManager: LinearLayoutManager

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

        transactions = arrayListOf(
            Transaction("Weekend budget", 1100.00),
            Transaction("Bananas", -30.00),
            Transaction("Apples", -80.00),
            Transaction("Breakfast", -150.00),
            Transaction("Juice", -30.00),
        )

        transactionAdapter = TransactionAdapter(transactions)
        linearLayoutManager = LinearLayoutManager(this)

        binding.recyclerview.apply {
            adapter = transactionAdapter
            layoutManager = linearLayoutManager
        }
        updateDashboard()
    }

    private fun updateDashboard() {

        val totalAmount = transactions.map{ it.amount }.sum()
        val budgetAmount = transactions.filter{ it.amount > 0 }.map{ it.amount }.sum()
        val expenseAmount = totalAmount - budgetAmount

        binding.totalBalance.text = "₹%.2f".format(totalAmount)
        binding.budgetLeft!!.text = "₹%.2f".format(budgetAmount)
        binding.totalExpense!!.text = "₹%.2f".format(expenseAmount)
    }
}

Please help me fix this.

I have tried adding dependencies and plugins in the gradle file. I have tried changing the variable names many times but that didn't work either.

  • 1
    Nullable references in your binding properties means you have more than one version of your layout, and those views are missing in at least one of the variants. – Tenfour04 Feb 27 '23 at 18:44

0 Answers0