EDIT Ok guys, I was literally trying to call the FAB which is inside the fragment_home, in another fragment, my mistake, I should've called inside the Homefragment, I just changed it and it works now, thank you to everyone that help.
TO ANYONE THAT GETS THE SAME ERROR -Check if you declared the ID of the view well -Check if you are inside that activity class (activity_main.xml -> MainActivity.kt) -Call the view correctly. IF that doesn't solve it, you will find great answers down below, good luck.
I have this fab, declared here:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseFragment"
android:background="@color/darkBlack">
<TextView
android:id="@+id/my_notes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:text="@string/notes"
android:textColor="@color/white"
android:textSize="@dimen/_20sdp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchIcon="@drawable/ic_search"
android:background="@drawable/background"
app:queryHint="Search..."
app:defaultQueryHint="Search..."
android:theme="@style/ThemeOverlay.search"
android:iconifiedByDefault="false"
app:layout_constraintTop_toBottomOf="@+id/my_notes"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="10dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_view" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/darkBlack"
android:orientation="horizontal"
android:padding="@dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_check_24"
app:tint="@color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_10sdp"
android:src="@drawable/ic_image"
app:tint="@color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_10sdp"
android:src="@drawable/ic_baseline_insert_link_24"
app:tint="@color/white" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/yellow"
android:id="@+id/fabBtnCreateNote"
android:tint="@color/yellow"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_20sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_add"
android:contentDescription="Fab" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am calling it here:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
abstract class BaseFragment() : Fragment(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = job +Dispatchers.Main
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var fab = view.findViewById<FloatingActionButton>(R.id.fabBtnCreateNote)
//Line throwing the error ->
fab.setOnClickListener{
replaceFragment(CreateNoteFragment.newInstance(), true)
}
}
open fun replaceFragment(fragment: Fragment, istransition:Boolean) {
val fragmentTransition = activity!!.supportFragmentManager.beginTransaction()
if(istransition) {
fragmentTransition.setCustomAnimations(android.R.anim.slide_out_right, android.R.anim.slide_in_left)
}
fragmentTransition.add(R.id.frame_layout, fragment).addToBackStack(fragment.javaClass.simpleName).commit()
}
}
I have declared it and am know calling it (I have android-kotlin-extensions) but it is throwing me a null object error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.floatingactionbutton.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
It is pointing to the fabBtnCreateNote.setOnClickListener{} line.
I am not understanding what this means, could anyone help me out?