There is a Fragment and on it buttons that need to identify a listener who would open different Activity . For example:

 bt_statistics.setOnClickListener { startActivity(Intent(this, StatisticsActivity::class.java)) } 

I understand that you need to call the Activity method from the fragment, which will launch the Activity , but how?

  • Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener (android.view.View $ OnClickListener)' on a null object reference figured out. - Vlad
  • one
    The fragment itself is perfectly able to launch activit would be intent. developer.android.com/reference/android/app/… - Eugene Krivenja 6:56 pm
  • val intent: Intent = Intent (this.activity, StatisticsActivity :: class.java) startActivity (intent) is the solution to my question. - Vlad

2 answers 2

Here:

 startActivity(Intent(this, StatisticsActivity::class.java)) 

The first parameter instead of the context you pass the fragment itself, which is not the context.

Call either like this:

 startActivity(Intent(context, StatisticsActivity::class.java)) 

Either way:

 startActivity(Intent(activity, StatisticsActivity::class.java)) 

    I will correct a little Peter Samokhin, startActivity (Intent ( getActivity (), StatisticsActivity :: class.java))