17

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.

4

2 回答 2

53

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

于 2012-06-16T04:14:56.003 回答
2

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.

于 2012-06-16T04:09:41.750 回答