-1

Just now google has made the Kotlin as an official programming language, so started converting my files to Kotlin.

While converting my code, I am always getting null pointer exception when I try to use supportActionBar

    // set up action bar
    setSupportActionBar(mToolbar)
    supportActionBar!!.setDisplayShowTitleEnabled(false)

    // set the hamburger menu
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    supportActionBar!!.setHomeButtonEnabled(true)

My AndroidManifest entry is

      <activity android:name=".activities.MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar />

No actionbar is having windowActionBar as false. Every time I run the app, i get the null pointer exception whiel accessing the supportActionBar. Can anyone help me about it?

DevAnuragGarg
  • 1,592
  • 2
  • 14
  • 18

2 Answers2

5

You can try

supportActionBar?.hide()
Rupesh Agrawal
  • 645
  • 8
  • 22
tj_
  • 75
  • 1
  • 11
  • 1
    While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Aug 31 '18 at 06:27
4

Have you initialised mToolbar with its view id? If not then make it like this and check.

var mToolbar = findViewById(R.id.toolbar) as Toolbar?
setSupportActionBar(mToolbar)
Maddy
  • 4,525
  • 4
  • 33
  • 53