Пожалуйста, помогите мне, я новичок в программировании kotlin. С этого утра у меня есть эта проблема, и я не могу ее решить. Я не понимаю ошибку, ... Это следующая строка кода: фрагмент.шоу (фрагментManager, "verifyDelete")
введите описание изображения здесь
MainActivity.kt
import android.app.PendingIntent.getActivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.graphics.Typeface
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.util.Log
import android.view.View
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val user = User("ACHAKA", "Eric", 26)
val button1 = findViewById<View>(R.id.button1)
button1.setOnClickListener {
println("Start 2nd activity")
val intent = Intent(this, GreenActivity::class.java)
intent.putExtra("user", user)
startActivity(intent)
}
findViewById<View>(R.id.button2).setOnClickListener{
val fragment = ConfirmDeleteDialogFragment()
fragment.listener = object: ConfirmDeleteDialogFragment.ConfirmDeleteListener {
override fun onDialogPositiveClick() {
Log.i("MainActivity", "onDialogPositiveClick()")
}
override fun onDialogNegativeClick() {
Log.i("MainActivity", "onDialogNegativeClick()")
}
}
fragment.show(fragmentManager,"confirmDelete")
}
}
}
ConfirmDeleteDialogFragment.kt
import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.DialogFragment
class ConfirmDeleteDialogFragment: DialogFragment() {
interface ConfirmDeleteListener {
fun onDialogPositiveClick()
fun onDialogNegativeClick()
}
val TAG = ConfirmDeleteDialogFragment::class.java.simpleName
var listener: ConfirmDeleteListener? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
builder.setMessage("Supprimer tout le contenu du téléphone ?")
.setPositiveButton("Oh oui !", object: DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, id: Int) {
Log.i(TAG, "Youpi ! on va tout casser")
listener?.onDialogPositiveClick()
}
})
.setNegativeButton("Euh... Non", DialogInterface.OnClickListener { dialog, id ->
Log.i(TAG, "Bon ben ce sera pour la prochaine fois")
dialog.dismiss()
listener?.onDialogNegativeClick()
})
return builder.create()
}
}
Logcat (ошибка)
:55:54.868 25808-25808/aea.com E/Zygote: v2
2019-12-23 00:55:54.883 25808-25808/aea.com E/Zygote: accessInfo : 0
2019-12-23 00:55:57.485 25808-25808/aea.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: aea.com, PID: 25808
java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.fragment.app.FragmentTransaction androidx.fragment.app.FragmentManager.beginTransaction()' on a null object reference
at androidx.fragment.app.DialogFragment.show(DialogFragment.java:142)
at aea.com.MainActivity$onCreate$2.onClick(MainActivity.kt:42)
at android.view.View.performClick(View.java:6207)
at android.view.View$PerformClick.run(View.java:23639)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)