I was trying to display custom toast by on Long click of a button a default toast on click of a button but long click method is showing error line
here is the code
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btntoste.setOnClickListener {
Toast.makeText(this,"this is defult toaste",Toast.LENGTH_SHORT)
}
btntoste.setOnLongClickListener {
Toast(this).apply {
duration = Toast.LENGTH_LONG
view = layoutInflater.inflate(R.layout.custom_toaste, layconstrain)
show()
}
}
}
}
also the app is crash while lunching
here is the logcat error
failed to add asset path /data/app/com.sumita.toaste-wg1Yd4-KC7e1sn36eDPchA==/base.apk
2023-05-10 11:58:44.858 18737-18737 ResourcesManager pid-18737 E failed to add asset path /data/app/com.sumita.toaste-wg1Yd4-KC7e1sn36eDPchA==/base.apk
2023-05-10 11:58:44.861 18737-18737 ResourcesManager pid-18737 E failed to add asset path /data/app/com.sumita.toaste-wg1Yd4-KC7e1sn36eDPchA==/base.apk
2023-05-10 11:58:45.088 2461-2501 ActivityThread com.sumita.toaste E Failed to find provider info for cn.teddymobile.free.anteater.den.provider
2023-05-10 11:58:45.229 2461-2461 AndroidRuntime com.sumita.toaste E FATAL EXCEPTION: main
Process: com.sumita.toaste, PID: 2461
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sumita.toaste/com.sumita.toaste.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2994)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3072)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1766)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.sumita.toaste.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:7227)
at android.app.Activity.performCreate(Activity.java:7218)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3072)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1766)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
activity_main.xml file
<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=".MainActivity">
<Button
android:id="@+id/btntoste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toste"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.266"
android:backgroundTint="@color/material_dynamic_neutral50"
android:textColor="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
custom toast layout
<?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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layconstrain"
android:padding="16dp"
android:background="@color/material_dynamic_neutral70">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_baseline_circle_notifications_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtcucont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Hay this is custom toaste"
android:textSize="15sp"
android:textColor="@color/purple_700"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.583" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is all the file I have for this project
2
Answers
You need to return boolean value in
setOnLongClickListener
method like that. I am using viewBinding to fetch view. Try thisAccording to the error, the
btntoste
variable has anull
value. That’s because you haven’t assigned thebtntoste
variable. To solve that:You also need to return a boolean value when using the
setOnLongClickListener()
function.