2013-03-11 62 views
0

我使用supportv4兼容庫,我的應用程序的目標API級別是17,我想使它即使在API級別4 當我運行我的應用程序我有這樣的錯誤工作僅在API級別4:Android的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" /> 

回答

1

API等級4並在課堂上使用的任何方法進行全面檢查,以確保它的存在,如注意this answer。因此,您可以:

  • 封裝在單獨的類(其中,因爲它們不是在你的V4運行時加載由於條件邏輯,永遠不會貪婪地檢查)
  • 您的最低SDK移動到所有新的API調用V7(安卓2.1),失去了Android人口0.2%仍然使用V4。
+0

非常感謝您對這個信息,我注意到,因爲在那一刻達到代碼之前也引發錯誤當虛擬機加載類!如果只有0.2%的人口輸掉,我認爲我將使它僅適用於10級以上的api級別! – Meher 2013-03-11 16:34:11

+0

這裏你的邏輯(有條件執行的基礎上,SDK_INT新API)將在任何V7 +設備的工作。 注意,API V10 +應用達到目前Android人口的90.2%(每相同的儀表板)。 – ianhanniballake 2013-03-11 16:41:00

+0

是的,我meaned API 7級,我測試了我的應用程序在Android 2.1的模擬器它完美的作品,再次感謝您! – Meher 2013-03-12 09:06:46