title: getting error as Caused by: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
body: <p>I was building a city guide app where i used viewpager2 for swiping of pages left and right. It was running with 0 error until I added the this code which is given below . And got a error saying   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sahiladhav.cityguide/com.sahiladhav.cityguide.Common.OnBoarding}: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager.<br />
  What should I do??</p>
  <pre><code> &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout 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;.Common.OnBoarding&quot;
      android:padding=&quot;20dp&quot;&gt;
  
  
      &lt;androidx.viewpager2.widget.ViewPager2
          android:id=&quot;@+id/slider&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          app:layout_constraintBottom_toTopOf=&quot;@+id/relativelayout&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent &quot;&gt;
  
      &lt;/androidx.viewpager2.widget.ViewPager2&gt;
  
      &lt;RelativeLayout
          android:id=&quot;@+id/relativelayout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;150dp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
  
              &lt;Button
                  android:id=&quot;@+id/get_started_btn&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;@string/let_get_started&quot;
                  android:background=&quot;@color/colorPrimary&quot;/&gt;
  
              &lt;LinearLayout
                  android:id=&quot;@+id/dots&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_alignParentBottom=&quot;true&quot;
                  android:padding=&quot;20dp&quot;
                  android:orientation=&quot;horizontal&quot; /&gt;
          
              &lt;Button
                  android:id=&quot;@+id/next_btn&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_alignParentBottom=&quot;true&quot;
                  android:layout_alignParentRight=&quot;true&quot;
                  android:paddingBottom=&quot;20dp&quot;
                  android:background=&quot;#00000000&quot;
                  android:drawableEnd=&quot;@drawable/next_btn&quot;
                  android:drawableRight=&quot;@drawable/next_btn&quot; /&gt;
          
  
  
      &lt;/RelativeLayout&gt;
  
  
  
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>MY JAVA CODE</p>
  <pre><code>package com.sahiladhav.cityguide.Common;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.viewpager.widget.ViewPager;
  import androidx.viewpager2.widget.ViewPager2;
  
  import android.os.Bundle;
  import android.text.Html;
  import android.transition.Slide;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import com.sahiladhav.cityguide.HelperClasses.SliderAdapter;
  import com.sahiladhav.cityguide.R;
  
  public class OnBoarding extends AppCompatActivity {
  
      ViewPager viewPager;
      LinearLayout dotsLayout;
  
      SliderAdapter sliderAdapter;
      TextView[] dots;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_on_boarding);
  
          //Hooks
          viewPager = findViewById(R.id.slider);
          dotsLayout = findViewById(R.id.dots);
  
          //Call adapter
          sliderAdapter = new SliderAdapter(this);
          viewPager.setAdapter(sliderAdapter);
          addDots();
  
      }
  
      private void addDots(){
  
          dots = new TextView[4];
          for (int i=0; i&lt;dots.length; i++){
              dots[i] = new TextView(this);
              dots[i].setText(Html.fromHtml(&quot;&amp;#8226;&quot;));
              dots[i].setTextSize(35);
  
              dotsLayout.addView(dots[i]);
          }
  
  
  
      }
  }
  </code></pre>
  <p>LOGS I AM GETTING , HOW CAN I SOLVE THIS ERROR , I CANT MOVE CODE FURTHER BEFORE SOLVING THIS..SOMEONE HELP ME PLEASE :((</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.sahiladhav.cityguide, PID: 10535
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sahiladhav.cityguide/com.sahiladhav.cityguide.Common.OnBoarding}: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:223)
          at android.app.ActivityThread.main(ActivityThread.java:7656)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
       Caused by: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
          at com.sahiladhav.cityguide.Common.OnBoarding.onCreate(OnBoarding.java:30)
          at android.app.Activity.performCreate(Activity.java:8000)
          at android.app.Activity.performCreate(Activity.java:7984)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:223) 
          at android.app.ActivityThread.main(ActivityThread.java:7656) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
  </code></pre>