0

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>
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Do you have multiple versions of your layout for different screen orientations/densities? Maybe the card view is missing in one of them. – Tenfour04 Mar 17 '22 at 14:38
  • Currently, I have only one version of the layout, but I'm having multiple card view – Christopher Chua Mar 17 '22 at 15:01
  • Start by identifying the line where the exception occurs. You can find this by reading the rest of the error message that you didn't post here. – Code-Apprentice Mar 19 '22 at 01:12
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Mar 19 '22 at 01:12
  • Hi, I solved this problem by putting the setOnClickListener inside the fragment class. Thanks for the helps anyway. – Christopher Chua Mar 19 '22 at 07:23

0 Answers0