微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为更高版本添加语句?

如何解决为更高版本添加语句?

| 我正在使用1.6即API 4来构建我的应用程序。较高版本支持几个命令。我想编写这些命令并使应用程序与更高版本的版本更加兼容。就像,我使用制表符。我想使用setLeftStripDrawable和setRightStripDrawable,但API 8支持它们。 我写类似:
// I want these lines to come into affect only if the device SDK is greater than 7 as SDK of below 7,doesn\'t support these methods.
if (android.os.Build.VERSION.SDK_INT > 7) {   
    tw.setLeftStripDrawable(R.drawable.tab_selected_bar_left_v4);  // TabWidget
}
编辑:我想将setLeftStripDrawable设置为我的应用程序中使用的选项卡。在我的清单中,我使用了-sdk android:minSdkVersion = \“ 4 \”。如果我按上述方式编写这些行并在2.3中进行编译,则编译成功。当我在1.6中运行时,得到\“ java.lang.VerifyError \”。如果我删除了这些留置权,然后再次在1.6中运行,它将正常工作。 仅当设备SDK api> 7时,才应该执行这些行,如果小于此值,则这些行不应受到任何影响? 有什么线索吗?     

解决方法

我认为您应该使用这样的东西。我是认真做的,所以可能会有一些错误。
try {
    Method twMethod = TabWidget.class.getMethod(\"setLeftStripDrawable\",new Class[] { int.class });
    twMethod.invoke(tw,R.drawable.yourdrawable);
} catch (NoSuchMethodException e) {
    /* not supported */
} catch (IllegalArgumentException e) {
    /* wrong class provided */
} catch (IllegalAccessException e) {
    /* Java access control has denied access */
} catch (InvocationTargetException e) {
    /* method has thrown an exception */
}
    ,
if (Build.VERSION.SDK_INT > 7) {
    ...
}
    ,您可以尝试查看Android Reflection。我本人还没有使用过它,但是据我了解,您可以测试您知道名称的类和方法。然后,您可以实例化并使用它们。 您可以在此处阅读一些基础知识:http://www.tutorialbin.com/tutorials/85977/java-for-android-developers-reflection-basics     ,这是一些使用反射的示例Android代码,它们执行类似的操作。它从Display类调用getRotation()方法。该方法仅在SDK 8+中存在。我已经在我的一个应用程序中使用了它,并且可以正常工作:
    //I want to run this:  displayrotation = getWindowManager().getDefaultDisplay().getRotation();
    //but the getRotation() method only exists in SDK 8+,so I have to call it in a sly way,using Java \"reflection\"
    try{
        Method m = Display.class.getMethod(\"getRotation\",(Class[]) null);//grab the getRotation() method if it exists
        //second argument above is an array containing input Class types for the method. In this case it takes no inputs.
        displayrotation = (Integer) m.invoke(getWindowManager().getDefaultDisplay(),(Object[]) null);
        //again,second argument is an array of inputs,in this case empty
    }catch(Exception e){//if method doesn\'t exist,take appropriate alternate actions
        Log.w(\"getRotation\",\"old OS version => Assuming 90 degrees rotation\");
        displayrotation = Surface.ROTATION_90;
    }
    ,尝试查看此问题,以编程方式获取Android API版本     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。