0

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
  • 20,406
  • 11
  • 52
  • 83
Asad Fiaz
  • 105
  • 1
  • 7

4 Answers4

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
  • 864
  • 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
  • 835
  • 6
  • 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.

0

Ensure that you don't manually create the class or the xml for the MainActivity. Create it by right clicking on com.example.filename > New > Activity.

John
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33913807) – Maveňツ Mar 01 '23 at 11:30