Android ClassNotFoundException with ViewPager

我收到以下错误,我不明白...

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

这是我的代码...

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);
}

这是一个滚动列表。我正在用这段代码扩展和膨胀视图...ListAdapterViewPagerBaseAdapter

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

有人可以帮我了解出了什么问题吗?


答案 1

您的 XML 在布局文件中的外观如何?如果你只是把

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

您的应用程序将崩溃,因为它不与其他小部件一起位于核心 SDK 包中,而是位于支持库中。您需要输入完全限定的名称ViewPager

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

此外,正如其他人所提到的,请确保支持库JAR已正确包含在项目中

呵呵


答案 2

我认为你在那里有太多的前缀。super

试试这个:

@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);   
}   

此外,请确保 位于名为 (该目录应位于项目中,并且与 and 和 位于同一级别)中。从ADT 17(IIRC)开始,这是必须的。只需创建该目录,将.jar放在那里,Eclipse就会处理其余部分,而无需尝试将其手动添加到构建路径或其他任何内容中。android-support-v4.jarlibsressrcassets


推荐