Android和放大器;调用方法,目前API可是没有 [英] Android & calling methods that current API doesnt have
问题描述
如果我想要得到这样的外部路径和设备具有的Android 2.1(API 7)
If i want to get the external path like this, and device has Android 2.1 (api 7)
File f;
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
if (sdkVersion >= 8) {
System.out.println(">=8");
f = getApplicationContext().getExternalFilesDir(null);
} else {
System.out.println("<=7");
f = Environment.getExternalStorageDirectory();
}
LogCat中会显示:
LogCat will display:
05-25 15:44:08.355: W/dalvikvm(16688): VFY: unable to resolve virtual method 12: Landroid/content/Context;.getExternalFilesDir (Ljava/lang/String;)Ljava/io/File;
,但应用程序不会起皱。我想知道什么是VFY?有什么虚拟机Dalvik的来检查,如果一个叫做方法内部code是有效的?由于当前凸出编译agains的Android 2.2的Eclipse所以没有抱怨..但在运行时,我得到LogCat中输入
, but app will not crush. I want to know what is VFY? Is there something in the virtual machine dalvik that checks if code inside a called method is valid? Because current proj was compiled agains Android 2.2 so Eclipse didn't complained.. but at runtime, i get LogCat entry
PS:我不使用方法像这样真的,我有一个初始化类API&LT Helper类= 7或另一个API> = 8 ..但还是请回答
PS: i dont use method like this in really, i have Helper class which initialises a class for API<=7 or another for API>=8.. but still please answer!
推荐答案
是 VFY
错误是由DEX验证登录的Dalvik。
Yes, VFY
errors are logged from dex verifier in dalvik.
您是因为你的SDK版本进行运行时检查,并调用API方法面临着这个问题。问题是,即使方法调用中的如果(){}
这可能永远不会在较低的API级别执行的块,符号信息为present在生成的字节code。如果您需要执行平台特定的函数调用,你需要使用反射。
You are facing this issue because you are performing runtime checks for the SDK version and calling the API methods. The problem is even if the method call is inside the if(){}
block which may never be executed in lower API levels, the symbolic information is present in the generated bytecode. If you need to perform platform specific function calls, you need to use reflection.
File f;
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
if (sdkVersion >= 8) {
System.out.println(">=8");
try {
Method getExternalFilesDir = Context.class.getMethod("getExternalFilesDir", new Class[] { String.class } );
f = (File)getExternalFilesDir.invoke(getApplicationContext(), new Object[]{null});
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("<=7");
f = Environment.getExternalStorageDirectory();
}
这篇关于Android和放大器;调用方法,目前API可是没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!