怎么理解Android版本兼容???
程序中设置了minSdkVersion和targetSdkVersion如下:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23"/>
设备的版本信息如下:
运行程序出错,Log如下:
01-06 13:56:11.798: E/AndroidRuntime(2396): FATAL EXCEPTION: main
01-06 13:56:11.798: E/AndroidRuntime(2396): java.lang.NoSuchMethodError: android.view.Display.getSize
01-06 13:56:11.798: E/AndroidRuntime(2396): at com.xxx.xxx.activity.TutorialActivity.init(TutorialActivity.java:73)
01-06 13:56:11.798: E/AndroidRuntime(2396): at com.xxx.xxx.activity.TutorialActivity.onResume(TutorialActivity.java:64)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.Activity.performResume(Activity.java:4004)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2515)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2546)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1859)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.os.Looper.loop(Looper.java:150)
01-06 13:56:11.798: E/AndroidRuntime(2396): at android.app.ActivityThread.main(ActivityThread.java:4333)
01-06 13:56:11.798: E/AndroidRuntime(2396): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 13:56:11.798: E/AndroidRuntime(2396): at java.lang.reflect.Method.invoke(Method.java:507)
01-06 13:56:11.798: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
01-06 13:56:11.798: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
01-06 13:56:11.798: E/AndroidRuntime(2396): at dalvik.system.NativeStart.main(Native Method)
查看官方文档得知:
该方法是在Android 3.2加入的,问题来了,你让3.2之前的用户怎么办?编写代码时IDE也没有提示说这个方法不能用啊。
对于Android的这个版本兼容性问题真心不想吐槽了,最终解决问题的方法是:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { // 包含新API的代码块 } else { // 包含旧的API的代码块 }
蛋碎了一地。