遇见引入ViewPager包,编译不报错运行报错ViewPager不存在问题
今天遇到一个比较奇怪的问题:导入android.support.v4.view.ViewPager包,引入ViewPager,编译不报错,运行报错ViewPager不存在问题。
由于工程的Target是Android2.3平台,而且代码是从之前的一个原有包里导入的。想加如一个viewpager.添加过程一切顺利,但是运行是报错:
07-23 15:43:16.980: D/AndroidRuntime(4897): Shutting down VM 07-23 15:43:16.980: W/dalvikvm(4897): threadid=1: thread exiting with uncaught exception (group=0x4001d578) 07-23 15:43:17.000: E/AndroidRuntime(4897): FATAL EXCEPTION: main 07-23 15:43:17.000: E/AndroidRuntime(4897): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample/com.sample.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v4.view.ViewPager 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread.access$1500(ActivityThread.java:121) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.os.Handler.dispatchMessage(Handler.java:99) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.os.Looper.loop(Looper.java:130) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread.main(ActivityThread.java:3714) 07-23 15:43:17.000: E/AndroidRuntime(4897): at java.lang.reflect.Method.invokeNative(Native Method) 07-23 15:43:17.000: E/AndroidRuntime(4897): at java.lang.reflect.Method.invoke(Method.java:507) 07-23 15:43:17.000: E/AndroidRuntime(4897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853) 07-23 15:43:17.000: E/AndroidRuntime(4897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611) 07-23 15:43:17.000: E/AndroidRuntime(4897): at dalvik.system.NativeStart.main(Native Method) 07-23 15:43:17.000: E/AndroidRuntime(4897): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v4.view.ViewPager 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 07-23 15:43:17.000: E/AndroidRuntime(4897): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:211) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.Activity.setContentView(Activity.java:1657) 07-23 15:43:17.000: E/AndroidRuntime(4897): at com.sample.MainActivity.onCreate(MainActivity.java:70) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630) 07-23 15:43:17.000: E/AndroidRuntime(4897): ... 11 more 07-23 15:43:17.000: E/AndroidRuntime(4897): Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager in loader dalvik.system.PathClassLoader[/data/app/com.sample-1.apk] 07-23 15:43:17.000: E/AndroidRuntime(4897): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 07-23 15:43:17.000: E/AndroidRuntime(4897): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 07-23 15:43:17.000: E/AndroidRuntime(4897): at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.createView(LayoutInflater.java:471) 07-23 15:43:17.000: E/AndroidRuntime(4897): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570) 07-23 15:43:17.000: E/AndroidRuntime(4897): ... 20 more 07-23 15:43:18.570: I/Process(4897): Sending signal. PID: 4897 SIG: 9
Java代码中:
import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener;
并在onCreate()方法中:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ...... }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ViewPager android:id="@+id/viewpagerLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
1、在项目的properties中的Java Build Path里将Order and Export里的android-support-v4.jar勾选上
2、由于ADT版本问题,最简单的解决办法 新建libs 把包放进去然后点击libs右键-》useas source folder
3、因为adt升级了,lib文件夹 要用libs命名,那样就会自动引入到android dependencies中因为support.v4是google自带的附加包,不是自定义包,所以引入到android dependencies中。
4、在xml布局文件中引用要使用全路径,即android.support.v4.view.ViewPager
然而以上方法均不能解决。
最后折腾了1个多小时,终于解决问题。
对于这个问题,需要注意的点如下:
第一:正确导入android.support.v4.jar
如果是新建项目,一般不会存在这个问题,出现这个问题的,大部分应该是从老项目中导入的。或者ADT没有升级过,一直在开发Android2.x的应用,现在升级了ADT,要去开发高版本的应用。
针对已经导入的项目,首先关闭eclipse,删除项目目录下的libs目录;--》打开eclipse,在项目的properties中的Java Build Path里将Libraries中将有红色叉叉的库给remove掉;--》Add External JARs…将android.support.v4.jar添加进来。
第二:在项目的properties中的Java Build Path里将Order and Export里的android-support-v4.jar勾选上.
第三:在xml布局文件中引用要使用全路径,即android.support.v4.view.ViewPager;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 此处需要给出全路径 --> <android.support.v4.view.ViewPager android:id="@+id/viewpagerLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
按此操作之后,问题解决。
参考:
http://ask.csdn.net/questions/1525
http://www.haogongju.net/art/1579482
http://www.apkbus.com/android-73371-1-1.html
http://blog.csdn.net/lyk498837077/article/details/9372073