0

我正在使用 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" />
4

1 回答 1

1

API 级别 4 对类中使用的每个方法进行全面检查,以确保它存在,如本答案中所述。因此,您可以:

  • 将所有新的 API 调用封装在单独的类中(由于条件逻辑,它们不会在 v4 运行时中加载,因此永远不会被贪婪地检查)
  • 将您的最低 SDK 移至 v7 (Android 2.1),并失去0.2%的 Android 用户仍在使用 v4。
于 2013-03-11T16:27:51.020 回答