title: Getting RecyclerView to Display in Tablayout Fragments
body: <p>Actually am trying to add recyclerView to a tabLayout fragment which for a while i have not been able to get it working, as it gives an error each time i tried. AM new to android development and i dont really seem to know where am getting things messed up</p>
  <h2><strong>XML CODES</strong> ||</h2>
  <p><strong>Activity_main.xml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;
      android:id=&quot;@+id/drawer_layout&quot;&gt;
  
      &lt;RelativeLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:background=&quot;#F7F7F7&quot;&gt;
          &lt;include layout=&quot;@layout/navaction_bar&quot;
                   android:layout_width=&quot;match_parent&quot;
                   android:layout_height=&quot;wrap_content&quot; /&gt;
  
          &lt;com.google.android.material.tabs.TabLayout
              android:id=&quot;@+id/tb_layout&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_alignParentTop=&quot;true&quot;
              android:layout_alignParentEnd=&quot;true&quot;
              android:layout_marginTop=&quot;253dp&quot;
              android:layout_marginRight=&quot;16dp&quot;
              android:layout_marginLeft=&quot;16dp&quot;
              android:background=&quot;@drawable/tab_layout_background&quot;
              app:tabIndicatorColor=&quot;@color/c4&quot;
              app:tabTextColor=&quot;#000000&quot;
              app:tabMode=&quot;fixed&quot;
              app:tabGravity=&quot;fill&quot;
              android:elevation=&quot;1dp&quot;&gt;
  
  
          &lt;/com.google.android.material.tabs.TabLayout&gt;
  
          &lt;androidx.viewpager.widget.ViewPager
              android:id=&quot;@+id/v_pager&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;600dp&quot;
              android:layout_alignBottom=&quot;@+id/tb_layout&quot;
              android:layout_alignParentEnd=&quot;true&quot;
              android:layout_alignParentBottom=&quot;true&quot;
              android:layout_marginTop=&quot;304dp&quot;
              android:layout_marginLeft=&quot;16dp&quot;
              android:layout_marginRight=&quot;16dp&quot;
              android:background=&quot;#ffffff&quot;
              android:elevation=&quot;1dp&quot; &gt;
  
          &lt;/androidx.viewpager.widget.ViewPager&gt;
  
  
      &lt;/RelativeLayout&gt;
  
      &lt;com.google.android.material.navigation.NavigationView
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;match_parent&quot;
          app:menu=&quot;@menu/navigation_menu&quot;
          android:layout_gravity=&quot;start&quot;
          app:headerLayout=&quot;@layout/navigation_header&quot;&gt;
  
      &lt;/com.google.android.material.navigation.NavigationView&gt;
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  
  </code></pre>
  <p><strong>services_fragment.xml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:gravity=&quot;start&quot;&gt;
  
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recycler_view&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p><strong>list_adapter.xml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/parent_layout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:background=&quot;#fff&quot;
      android:padding=&quot;15dp&quot;&gt;
  
      &lt;de.hdodenhof.circleimageview.CircleImageView
          android:id=&quot;@+id/circle_img&quot;
          android:layout_width=&quot;80dp&quot;
          android:layout_height=&quot;80dp&quot;
          android:src=&quot;@drawable/img_20190413_083028&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/image_name&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_marginLeft=&quot;30dp&quot;
          android:layout_toRightOf=&quot;@+id/circle_img&quot;
          android:text=&quot;Courage&quot;
          android:textColor=&quot;#000&quot;
          android:textSize=&quot;17sp&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <h2><strong>JAVA CODES ||</strong></h2>
  <p><strong>MainActivity.java</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.example.drawerlayout;
  
  import androidx.appcompat.app.ActionBarDrawerToggle;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.drawerlayout.widget.DrawerLayout;
  
  import android.os.Bundle;
  import android.util.Log;
  import android.view.MenuItem;
  import android.widget.ListView;
  
  import androidx.appcompat.widget.Toolbar;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  import androidx.viewpager.widget.ViewPager;
  
  import com.google.android.material.appbar.AppBarLayout;
  import com.google.android.material.tabs.TabLayout;
  
  import java.util.ArrayList;
  
  
  public class MainActivity extends AppCompatActivity {
      private static final String TAG = &quot;MainActivity&quot;;
      private DrawerLayout mDrawerLayout;
      private ActionBarDrawerToggle mToggle;
      private Toolbar mToolbar;
      private AppBarLayout actionLayout;
      private TabLayout tabLayout;
      private ViewPager viewPager;
      private ArrayList&lt;String&gt; mNames = new ArrayList&lt;&gt;();
  
      private ArrayList&lt;String&gt; mImageUrls = new ArrayList&lt;&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.d(TAG, &quot;onCreate: started&quot;);
          /*get state id*/
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
          mToolbar = (Toolbar) findViewById(R.id.nav_action);
          actionLayout = (AppBarLayout) findViewById(R.id.appbar);
          tabLayout = (TabLayout) findViewById(R.id.tb_layout);
          viewPager = (ViewPager) findViewById(R.id.v_pager);
  
  
          ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
  
  
          /*adding fragments*/
          viewPagerAdapter.AddFragment(new ServicesFragment(),&quot;Services&quot;); // `new ServicesFragment()` should be inside `FragmentPagerAdapter.getItem()`
          viewPagerAdapter.AddFragment(new UtilityFragment(),&quot;Utility Bills&quot;); // `new UtilityFragment()` should be inside `FragmentPagerAdapter.getItem()`
          viewPagerAdapter.AddFragment(new HistoryFragment(),&quot;History&quot;); // `new HistoryFragment()` should be inside `FragmentPagerAdapter.getItem()`
  
  
          // adapter setup
          viewPager.setAdapter(viewPagerAdapter);
          tabLayout.setupWithViewPager(viewPager);
  
          setSupportActionBar(mToolbar);
  
          mDrawerLayout.addDrawerListener(mToggle);
          mToggle.syncState();
  
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
  
          getSupportActionBar().setElevation(0);
          initImageBitmaps();
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          if (mToggle.onOptionsItemSelected(item)) return true;
          return super.onOptionsItemSelected(item);
      }
  
  
      private void initImageBitmaps(){
          Log.d(TAG, &quot;initImageBitmaps: bitmap started&quot;);
  
          mImageUrls.add(&quot;https://nationalwire.com.ng/wp-content/uploads/2017/08/glo-logo.jpg&quot;);
          mNames.add(&quot;MTN LOGO&quot;);
          mImageUrls.add(&quot;https://seeklogo.com/images/M/mtn-logo-A285C69508-seeklogo.com.png&quot;);
          mNames.add(&quot;GLO LOGO&quot;);
  
          initRecyclerView ();
      }
  
      private void initRecyclerView () {
          Log.d(TAG, &quot;initRecyclerView: init RecyclerView&quot;);
          RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
          RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, mImageUrls, mNames);
          recyclerView.setAdapter(recyclerViewAdapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
      }
  }
  
  </code></pre>
  <p><strong>ServicesFragment.java</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.example.drawerlayout;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ListView;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  
  public class ServicesFragment extends Fragment {
      View view;
      ListView lView;
      public ServicesFragment() {
      }
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.services_fragment, container, false);
          return view;
      }
  }
  </code></pre>
  <p><strong>RecyclerViewAdapter.java</strong></p>
  <pre class="lang-java prettyprint-override"><code>  package com.example.drawerlayout;
  
  import android.content.Context;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.bumptech.glide.Glide;
  
  import java.util.ArrayList;
  
  import de.hdodenhof.circleimageview.CircleImageView;
  
  public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt; {
      private static final String TAG = &quot;RecyclerViewAdapter&quot;;
  
      private ArrayList&lt;String&gt; mImages = new ArrayList&lt;&gt;();
      private ArrayList&lt;String&gt; mImageNames = new ArrayList&lt;&gt;();
      private Context mContext;
  
      public RecyclerViewAdapter(Context mContext, ArrayList&lt;String&gt; mImages, ArrayList&lt;String&gt; mImageNames) {
          this.mImages = mImages;
          this.mImageNames = mImageNames;
          this.mContext = mContext;
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_adapter, parent, false);
          ViewHolder holder = new ViewHolder(view);
          return holder;
      }
  
      @Override
      public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
          Log.d(TAG, &quot;onBindViewHolder: called.&quot;);
  
          Glide.with(mContext)
                  .asBitmap()
                  .load(mImages.get(position))
                  .into(holder.image);
          holder.image_name.setText(mImageNames.get(position));
  
          holder.parentLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Log.d(TAG, &quot;onClick: clicked on: &quot; + mImageNames.get(position));
  
                  Toast.makeText(mContext, mImageNames.get(position), Toast.LENGTH_SHORT).show();
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return mImageNames.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder {
          CircleImageView image;
          TextView image_name;
          RelativeLayout parentLayout;
          public ViewHolder(@NonNull View itemView) {
              super(itemView);
              image = itemView.findViewById(R.id.circle_img);
              image_name = itemView.findViewById(R.id.image_name);
              parentLayout = itemView.findViewById(R.id.parent_layout);
          }
      }
  }
  
  </code></pre>
  <h2><strong>ERROR LOG</strong></h2>
  <pre class="lang-java prettyprint-override"><code>  --------- beginning of crash
  09-11 20:00:22.923 3573-3573/com.example.drawerlayout E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.drawerlayout, PID: 3573
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawerlayout/com.example.drawerlayout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
          at android.app.ActivityThread.access$800(ActivityThread.java:153)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5293)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
          at com.example.drawerlayout.MainActivity.initRecyclerView(MainActivity.java:97)
          at com.example.drawerlayout.MainActivity.initImageBitmaps(MainActivity.java:90)
          at com.example.drawerlayout.MainActivity.onCreate(MainActivity.java:71)
          at android.app.Activity.performCreate(Activity.java:5990)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
          at android.app.ActivityThread.access$800(ActivityThread.java:153) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:135) 
          at android.app.ActivityThread.main(ActivityThread.java:5293) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:372) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
  09-11 20:05:23.322 3573-3573/com.example.drawerlayout I/Process: Sending signal. PID: 3573 SIG: 9
  </code></pre>