gpt4 book ai didi

android - 未考虑 TargetApi

转载 作者:IT老高 更新时间:2023-10-28 22:19:55 51 4
gpt4 key购买 nike

在我们的一种方法中,我们在 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/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com