-1

I made a new project using navigation drawer that android gives me as builtin and then i added my menus in the navigation Drawer and then made another activity that is empty activity and made a button over there of getStarted and on the click listening i made an intent of the mainActivity that has the navigation drawer and then assigned this to the getstarted button to startActivity(mainIntent) but its not working, I have done many R&D but didn't work at all and i am getting following errors.

I have tried all the discussion over here

but nothing is working in my case

class WelcomeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        getStartedBtn.setOnClickListener {
            val main = Intent(this,MainActivity::class.java)
            startActivity(main)
        }
    }

I want to be routed to the main activity.In main activity I have used builtin toolbar or navigation drawer.

ADM
  • 18,477
  • 11
  • 47
  • 78
Asad Fiaz
  • 85
  • 7

3 Answers3

1

To access this from an outer scope (a class, or extension function, or labeled function literal with receiver) we write this@label where @label is a label on the scope this is meant to be from:

You should specify your scope. For more information you can check this link.

val main = Intent(this@WelcomeActivity, MainActivity::class.java)
startActivity(main)

Hope this works!

Ensar Bayhan
  • 656
  • 10
  • 14
0

Try to change val main = Intent(this,MainActivity::class.java) to val main = Intent(WelcomeActivity.this,MainActivity::class.java)

Also make sure that getStartedBtn is correctly imported from xml file.

Maksim Novikov
  • 790
  • 5
  • 18
0

Do you imported getStartedBtn correctly and try to change the val main = Intent(this,MainActivity::class.java) to val main = Intent(applicatonContext,MainActivity::class.java) it thought this will help if you still facing any problem please post the error.