fragment.show(fragmentManager,“ confirmDelete”)错误

时间:2019-12-23 00:34:28

标签: android-studio kotlin dialogfragment

请帮助我,我是kotlin编程的新手。从今天早上开始,我遇到了这个问题,我无法解决。我不明白错误,...这是这行代码:fragment.show(fragmentManager,“ confirmDelete”)

enter image description here

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)

2 个答案:

答案 0 :(得分:2)

fragmentManager已过时:

    /**
     * Return the FragmentManager for interacting with fragments associated
     * with this activity.
     *
     * @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
     */
    @Deprecated
    public FragmentManager getFragmentManager() {
        return mFragments.getFragmentManager();
    }

因此,您必须使用supportFragmentManager而不是fragmentManager

如果您使用fragmentManager,它将不会被编译(如红线所示,它也显示在屏幕截图中)。

答案 1 :(得分:0)

谢谢 supportFragmentManager 很好地解决了我的问题。 :)

fragment.show(supportFragmentManager  ,"confirmDelete")