Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ClassNotFoundException with ViewPager

I am getting the following error, which I don't understand...

06-15 22:06:49.196: W/dalvikvm(17622): threadid=1: thread exiting with uncaught exception (group=0x40bcc1f8)
06-15 22:07:05.031: W/dalvikvm(17877): threadid=1: thread exiting with uncaught exception (group=0x40bcc1f8)
06-15 22:07:05.031: E/AndroidRuntime(17877): FATAL EXCEPTION: main
06-15 22:07:05.031: E/AndroidRuntime(17877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whipple.android/com.whipple.android.StockActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class ViewPager
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.os.Looper.loop(Looper.java:137)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread.main(ActivityThread.java:4512)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at java.lang.reflect.Method.invoke(Method.java:511)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at dalvik.system.NativeStart.main(Native Method)
06-15 22:07:05.031: E/AndroidRuntime(17877): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class ViewPager
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:272)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.Activity.setContentView(Activity.java:1851)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at com.whipple.android.StockActivity.onCreate(StockActivity.java:17)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.Activity.performCreate(Activity.java:4481)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
06-15 22:07:05.031: E/AndroidRuntime(17877):    ... 11 more
06-15 22:07:05.031: E/AndroidRuntime(17877): Caused by: java.lang.ClassNotFoundException: android.view.ViewPager
06-15 22:07:05.031: E/AndroidRuntime(17877):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
06-15 22:07:05.031: E/AndroidRuntime(17877):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
06-15 22:07:05.031: E/AndroidRuntime(17877):    ... 21 more

This is my code...

public class StockActivity extends Activity {
public ListAdapter listadapter;
private StockPageAdapter myAdapter;
private ViewPager myPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.main);

    this.myAdapter = new StockPageAdapter(this);
    this.myPager = (ViewPager)super.findViewById(R.id.pager);
    this.myPager.setAdapter(myAdapter);
}

It's a ListAdapter with a ViewPager to scroll through the lists. I'm extending BaseAdapter and inflating the view with this code...

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, null);

Could someone please help me to understand what is wrong.

like image 769
Dakota Jay Whipple Avatar asked Jun 16 '12 03:06

Dakota Jay Whipple


People also ask

What is ViewPager Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.

How do I configure ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

What is difference between ViewPager and RecyclerView?

basically in ViewPager you can scroll only one item at time (either left or right), and in RecyclerView you can scroll to any index. it all depends on your requirements how you want to use it.


2 Answers

What does your XML look like in the layout file? If you simply put

<ViewPager
    android:layout_width="..."
    android:layout_height="..." />

Your application will crash because ViewPager is not located in the core SDK package with other widgets, it's in the support library. You need to put the fully qualified name

<android.support.v4.view.ViewPager
    android:layout_width="..."
    android:layout_height="..." />

Also, as others have mentioned, make sure the support library JAR is properly included in your project.

HTH

like image 112
devunwired Avatar answered Oct 10 '22 02:10

devunwired


I think you have a few too many super prefixes there.

Try this:

@Override   
public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);   

    this.myAdapter = new StockPageAdapter(this);   
    this.myPager = (ViewPager)findViewById(R.id.pager);   
    this.myPager.setAdapter(myAdapter);   
}   

Also, make sure the android-support-v4.jar is in a directory called libs (which should be in your project and on the same level as res and src and assets). This is a must now as of ADT 17 (IIRC). Just create that directory, drop the .jar in there and Eclipse will take care of the rest, no need to try and add it manually to the build path or anything.

like image 41
Barak Avatar answered Oct 10 '22 02:10

Barak