- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在我们的一种方法中,我们在 ListView 中使用了 smoothScrolling。由于此方法在 API Level 8 (FROYO) 之前不可用,因此我们使用 TargetApi 注解来防止在之前的 SDK 版本中调用该方法。
如您所见,我们确实在类定义和使用类对象的语句中都使用 TargetApi 注释。这比需要的多。
我们的问题是TargetApi注解没有被考虑在内,导致我们的模拟器在ECLAIR(SDK 7)版本中崩溃。通过跟踪,我们才意识到,应该只在版本 8+ 中执行的代码,在版本 7 中也执行了。
我们错过了什么吗?
这段代码在监听器中:
@TargetApi(8)
private final class MyOnMenuExpandListener implements OnMenuExpandListener {
@Override
public void onMenuExpanded( int position ) {
doScrollIfNeeded( position );
}
@Override
public void onMenuCollapsed( int position ) {
doScrollIfNeeded( position );
}
protected void doScrollIfNeeded( int position ) {
if ( mListViewDocuments.getLastVisiblePosition() - 2 < position ) {
mListViewDocuments.smoothScrollToPosition( position + 1 );
}
}
}
监听器是这样注册的:
@TargetApi(8)
private void allowSmothScrollIfSupported() {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO ) {
//This if should not be necessary with annotation but it is not taken into account by emulator
Log.d( LOG_TAG, "Smooth scroll support installed." );
folderContentAdapter.setOnMenuExpandListener( new MyOnMenuExpandListener() );
}
}
顺便说一句,我们在 Debug模式下运行代码,所以问题与混淆删除注释无关。
最佳答案
@TargetApi
不会阻止任何代码的运行,它只是用于注释代码并防止新 API 的编译器错误,一旦您知道您只是有条件地调用它们。
你仍然需要添加一些类似的东西
if (Build.VERSION.SDK_INT > 7){
//...
}
关于android - 未考虑 TargetApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12211061/
我正在按照教程创建用于在 Android 中发布推文的 Twitter 客户端。但是我在以下行中收到错误“Build cannot be resolved to a variable”: @Targe
我为我们的应用添加了 Android Beam 支持。但是因为我们仍然支持 2.2 * cough cough die froyo, die gingerbread, cough * 我已经将类 Nf
在我们的一种方法中,我们在 ListView 中使用了 smoothScrolling。由于此方法在 API Level 8 (FROYO) 之前不可用,因此我们使用 TargetApi 注解来防止在
我的应用程序中有关于 StrictMode 的问题,并添加了基本上禁用 StrictModeHelper 的代码 fragment 。但是,Lint 现在提示 setThreadPolicy() 并建
我正在开发一个使用 API 19 和隐藏式字幕功能的应用程序,但并非我所有的设备都安装了 Kit kat,因此当我使用此 API 的某些内容时,我会这样说: @TargetApi(19) privat
我有一个应用使用 Retrofit 在 Android 客户端和服务器之间使用 clearText,而在 Android 9+ 中不允许使用 明文。 为了忽略这一点,我在 Manifest 中添加了
有时我会为某些可能运行我的应用程序的 Android 版本列出代码。使用最新 Android 工具的 Eclipse 仍然注意到我的 Manifest 文档允许较低版本的 Android,因此确定我的
我的 list 中有以下行: 但我收到一个 lint 警告: Attribute autoVerify is only used in API 23 and higher (current min
我在我的应用程序中使用@TargetApi(23)。 @TargetApi(23) @Override public void onAttach(Context context) {
RequiresApi 和 TargetApi 有什么区别? kotlin 中的示例: @RequiresApi(api = Build.VERSION_CODES.M) @TargetApi(Bui
我正在尝试定义样式 sans-serif-condensed normal 但是 fontFamily 不适用于我正在使用的 api 10。那么有没有办法在样式表中使用注释呢?比如
我正在 eclipse 中开发一个 android 应用程序。我的 ide 显示了生成此行的提示。我将其添加到我的代码中: @TargetApi(Build.VERSION_CODES.GINGERB
我想使用下一个代码为 4 之后的所有 Android API 应用 SharedPreferences。 /** * The apply method was introduced * in An
我的项目在 list 中包含事件声明,该声明使用tools:targetApi属性来忽略一些警告。具体来说: 但是,出乎意料的是,例如在切换分支之后,出现以下编译
为了抑制 API 级警告,我通常更喜欢使用 @RequiresApi,而不是 @TargetApi。 因为,根据 RequiresApi vs TargetApi android annotation
我正在尝试获取 retrolambda使用 gradle 构建的 Android。 java 8 的改变导致了一些 android 注释的问题。 我已经尝试了这些帖子中的几乎所有内容:(在阅读所有
方法 isHardwareDetected() 和 hasEnrolledFingerprints() 总是返回 false。我发现目标 API 24 工作正常,所以这只是 25 的问题。我想使用最新
int API_VERSION = 21; @TargetApi(API_VERSION)在Android中用于指定该方法/类支持API_VERSION及以下。 我们是否可以镜像类似的东西,指定仅支持
所以我更新到最新的 v0.8,似乎我在 android.annotation.TargetApi 导入时遇到错误。看起来这属于尚未发布的 Api 级别 16。或者有什么包裹可以得到这个吗? 问候, 最
正如您在下面的代码中看到的那样,我将目标 api 设置为 18,并且我运行了进程 getRunningTasks,但它给了我一个错误并关闭了我的应用程序。 @TargetApi(18) public
我是一名优秀的程序员,十分优秀!