我正在使用 supportv4 兼容性库,我的应用程序的目标 Api 级别是 17,我想让它即使在 Api 级别 4 中也能工作。当我运行我的应用程序时,我只在 api 级别 4 中出现此错误:
03-11 16:16:01.412: E/dalvikvm(302): Could not find method android.view.Display.getSize, referenced from method com.meher.tools.menulauncher.common.MyApplicationContext.setScreenDimensions
03-11 16:16:01.412: W/dalvikvm(302): VFY: unable to resolve virtual method 3065: Landroid/view/Display;.getSize (Landroid/graphics/Point;)V
03-11 16:16:01.412: W/dalvikvm(302): VFY: rejecting opcode 0x6e at 0x0018
03-11 16:16:01.412: W/dalvikvm(302): VFY: rejected Lcom/meher/tools/menulauncher/common/MyApplicationContext;.setScreenDimensions (Landroid/view/Display;)V
03-11 16:16:01.412: W/dalvikvm(302): Verifier rejected class Lcom/meher/tools/menulauncher/common/MyApplicationContext;
03-11 16:16:01.422: D/AndroidRuntime(302): Shutting down VM
03-11 16:16:01.422: W/dalvikvm(302): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
03-11 16:16:01.422: E/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
03-11 16:16:01.422: E/AndroidRuntime(302): java.lang.VerifyError: com.meher.tools.menulauncher.common.MyApplicationContext
03-11 16:16:01.422: E/AndroidRuntime(302): at com.meher.tools.menulauncher.MainActivity.onCreate(MainActivity.java:79)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
03-11 16:16:01.422: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-11 16:16:01.422: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:16:01.422: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
03-11 16:16:01.422: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-11 16:16:01.422: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-11 16:16:01.422: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
03-11 16:16:01.422: I/dalvikvm(302): threadid=7: reacting to signal 3
03-11 16:16:01.422: E/dalvikvm(302): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
这是引发错误的代码:
MainActivity.java :
// Init application context
myApplicationContext = new MyApplicationContext(this,
getWindowManager().getDefaultDisplay());
MyApplicationContext.java :
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@SuppressWarnings("deprecation")
public void setScreenDimensions(Display display) {
Point size = new Point();
if (android.os.Build.VERSION.SDK_INT < 13) {
this.screenWidth = display.getWidth();
this.screenHeight = display.getHeight();
} else {
try {
display.getSize(size);
this.screenWidth = size.x;
this.screenHeight = size.y;
} catch (java.lang.NoSuchMethodError ignore) { // Older device
this.screenWidth = display.getWidth();
this.screenHeight = display.getHeight();
}
}
}
AndroidManifest.mf:
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="17" />