英文:
button.setOnLongClickListener() function is not working
问题
Sure, here's the translated code portion:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btntoste.setOnClickListener {
Toast.makeText(this, "this is default toast", Toast.LENGTH_SHORT).show()
}
btntoste.setOnLongClickListener {
Toast(this).apply {
duration = Toast.LENGTH_LONG
view = layoutInflater.inflate(R.layout.custom_toast, layconstrain)
show()
}
true
}
}
}
Please note that I've translated the code as requested, but there might still be issues in your code related to the btntoste
and layconstrain
elements, which could lead to runtime errors. Make sure those elements are correctly defined in your XML layout file and initialized in your code.
英文:
I want to show a default toast if a button is clicked once and a custom toast if the button is long-clicked. The default toast works but the custom toast doesn't:
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()
}
}
}
}
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:
<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>
答案1
得分: 1
需要在 setOnLongClickListener
方法中返回布尔值。我正在使用 viewBinding 来获取视图。尝试这样做
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.umesh.testapp.databinding.ActivityMainBinding
import com.umesh.testapp.databinding.CustomToasteBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btntoste.setOnClickListener {
Toast.makeText(this, "这是默认吐司", Toast.LENGTH_SHORT)
}
binding.btntoste.setOnLongClickListener {
val customToasteBinding = CustomToasteBinding.inflate(layoutInflater)
Toast(applicationContext).apply {
duration = Toast.LENGTH_LONG
view = customToasteBinding.root
show()
}
return@setOnLongClickListener true
}
}
}
英文:
You need to return boolean value in setOnLongClickListener
method like that. I am using viewBinding to fetch view. Try this
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.umesh.testapp.databinding.ActivityMainBinding
import com.umesh.testapp.databinding.CustomToasteBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btntoste.setOnClickListener {
Toast.makeText(this,"this is defult toaste",Toast.LENGTH_SHORT)
}
binding.btntoste.setOnLongClickListener {
val customToasteBinding = CustomToasteBinding.inflate(layoutInflater)
Toast(applicationContext).apply {
duration = Toast.LENGTH_LONG
view = customToasteBinding.root
show()
}
return@setOnLongClickListener true
}
}
}
答案2
得分: 0
根据错误信息,btntoste
变量具有 null
值。这是因为您尚未为 btntoste
变量分配值。要解决这个问题:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 通过其id访问按钮
val toastBtn = findViewById<Button>(R.id.btntoste)
// 显示默认提示
toastBtn.setOnClickListener {
Toast.makeText(this, "这是默认提示", Toast.LENGTH_SHORT).show()
}
// 显示自定义提示
toastBtn.setOnLongClickListener {
Toast(this).apply {
duration = Toast.LENGTH_LONG
view = layoutInflater.inflate(R.layout.custom_toaste, layconstrain)
show()
}
return@setOnLongClickListener true
}
}
}
当使用 setOnLongClickListener()
函数时,您还需要返回一个布尔值。
英文:
According to the error, the btntoste
variable has a null
value. That's because you haven't assigned the btntoste
variable. To solve that:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Access the Button using its id
val toastBtn = findViewById<Button>(R.id.btntoste)
//Display default toast
toastBtn.setOnClickListener {
Toast.makeText(this,"this is defult toaste",Toast.LENGTH_SHORT)
}
//Display custom toast
toastBtn.setOnLongClickListener {
Toast(this).apply {
duration = Toast.LENGTH_LONG
view = layoutInflater.inflate(R.layout.custom_toaste, layconstrain)
show()
}
return@setOnLongClickListener true
}
}
}
You also need to return a boolean value when using the setOnLongClickListener()
function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论