I'm having the issue when trying to click on the card view (budgetView), it says that the card view is returning null, saying that I am referencing to a null object. May I know what I need to change because I have already assigned an ID for the card view.
Below are the errorr: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navController = navHostFragment.navController
val btmNavigation = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
val budgetView = findViewById<CardView>(R.id.budgetView)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.reportFragment,
R.id.fundFragment,
R.id.planningFragment,
R.id.profileFragment
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
btmNavigation.setupWithNavController(navController)
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show()
budgetView.setOnClickListener{
val intent = Intent(this, BudgetActivity::class.java)
startActivity(intent)
}
}
}
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
app:layout_constraintTop_toTopOf="parent">
<androidx.cardview.widget.CardView
android:id="@+id/budgetView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="358dp"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/budget_planing_line_icon_business_and_finance_vector_16389318" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Budgets"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>