title: IllegalStateException: RecyclerView is null inside of Fragment within NavigationDrawer activity
body: <p>I tried to make a RecyclerView that will go inside one of the two pages of my fragments. These pages are put inside a NavigationDrawer activity. The goal is to create something like the Play Store app homepage.</p>
  
  <p>But I found an error in this snippet of code on runtime. It says:</p>
  
  <pre><code>java.lang.IllegalStateException: mainMenu must not be null
      at com.example.MyApp.app.fragment.MainFragment.onCreate(MainFragment.kt:49)
  </code></pre>
  
  <p>Ive been looking at some SO threads, and they said that the layouts are not properly loaded. Which caused some elements not linked as it should. Another said in the comments that the problem is with the context not initialized properly. Which is not the case for me (rather it's the RecyclerView).</p>
  
  <p>Here are the links, hope they could be useful as a reference.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/47413094/8174459">TextView must not be null</a></li>
  <li><a href="https://stackoverflow.com/questions/47923271/illegalstateexception-in-kotlin-context-must-not-be-null#comment82814235_47923271">context must not be null</a></li>
  </ul>
  
  <p>After multiple-checks on my codes, I swear I've put the right layouts in. <strong>EDIT: I imported the</strong> <code>kotlinx.android.synthetic.main.^</code> <strong>package where I put this sign: ^${layout}.</strong> Here's some of my files (forgive me if this thread becomes too long):</p>
  
  <ul>
  <li><p><code>MainActivity.kt : AppCompatActivity()</code> <strong>^activity_main.*</strong></p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          // preparing the app bars
          setSupportActionBar(toolBar)
  
          // getting ready for the pages
          val pagerAdapter = MainPagerAdapter(
                  supportFragmentManager,
                  resources.getString(R.string.tab_main),
                  resources.getString(R.string.tab_chat)
          )
          pager.adapter = pagerAdapter
  
          // activating tabs
          tabLayout.setupWithViewPager(pager)
  
          val toggle = ActionBarDrawerToggle(
                  this, mainDrawer, toolbar,
                  R.string.navigation_drawer_open,
                  R.string.navigation_drawer_close)
  
          mainDrawer.addDrawerListener(toggle)
  
  
          navView.setNavigationItemSelectedListener(this)
          toggle.syncState()
  
  }
  </code></pre></li>
  <li><p><code>MainPagerAdapter.kt (fm: FragmentManager, private val page1: String, private val page2: String): FragmentPagerAdapter(fm)</code></p>
  
  <pre><code>override fun getItem(position: Int): Fragment? {
      return when (position) {
          0 -&gt; MainFragment()
          1 -&gt; ChatFragment()
          else -&gt; null
      }
  }
  
  override fun getCount() = 2
  
  override fun getPageTitle(position: Int): CharSequence? {
      return when (position) {
          0 -&gt; page1
          1 -&gt; page2
          else -&gt; null
      }
  }
  </code></pre></li>
  <li><p><code>MainFragment.kt : Fragment()</code> <strong>^content_main.*</strong></p>
  
  <pre><code>override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)
      : View? = inflater.inflate(R.layout.content_main, container)
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
  
      // this is the error
      mainMenu.layoutManager = LinearLayoutManager(this.context)
  
      mainMenu.adapter = MyAdapter(itemList) {
          toast("${it.name} selected")
      }
  }
  </code></pre></li>
  <li><p><code>MyAdapter.kt: RecyclerView.Adapter&lt;MyAdapter.MyHolder&gt;()</code> <strong>^item_custom.view.*</strong>(courtesy of <a href="https://antonioleiva.com/recyclerview-adapter-kotlin/" rel="noreferrer">Antonio Leiva</a>)</p>
  
  <pre><code>override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
      = GoodsHolder(parent.inflate(R.layout.item_custom))
  
  override fun getItemCount()
      = itemList.size
  
  override fun onBindViewHolder(holder: MyHolder, position: Int)
      = holder.bind(itemList[position], listener)
  
  
  class MyHolder(v: View): RecyclerView.ViewHolder(v){
      private val item: Item? = null
      private val view = v
  
      fun bind(item: Item, listener: (Item) -&gt; Unit)
              = with (itemView) {
          imgPic.setImageResource(item.pictureId)
          txtName.text = item.name
          txtPrice.text = item.price.toString()
  
      setOnClickListener { listener(item) }
      }
  }
  </code></pre></li>
  <li><p>content_main.xml</p>
  
  <pre><code>&lt;android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.example.MyApp.app.activity.MainActivity"
      &gt;
  
      &lt;!-- the RecyclerView that caused the runtime error --&gt;
      &lt;android.support.v7.widget.RecyclerView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/mainMenu"/&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre></li>
  <li><p>item_custom.xml (These code are inside a LinearLayout inside a CardView)</p>
  
  <pre><code>&lt;ImageView
      android:id="@+id/imgPic"
      android:layout_width="match_parent"
      android:layout_height="128dp"
      app:srcCompat="@drawable/ic_menu_gallery" /&gt;
  
  &lt;TextView
      android:id="@+id/txtName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Get it while it's hot!"
      android:layout_margin="@dimen/margin_small"
      android:layout_marginTop="@dimen/margin_medium"
      android:maxLines="2"
      android:ellipsize="end"
      android:textStyle="bold"
      /&gt;
  
  &lt;TextView
      android:id="@+id/txtPrice"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="$3.000.000"
      android:layout_marginLeft="@dimen/margin_small"
      android:layout_marginStart="@dimen/margin_small"
      android:layout_marginRight="@dimen/margin_small"
      android:layout_marginEnd="@dimen/margin_small"
      android:layout_marginBottom="@dimen/margin_medium"/&gt;
  </code></pre></li>
  <li><p>ChatFragment.kt: Fragment() (only contains onCreateView inflating content_main_chat.xml)</p></li>
  <li><p>content_main_chat.xml (only contains a TextView)</p></li>
  </ul>