TargetApi不考虑 [英] TargetApi not taken into account

查看:327
本文介绍了TargetApi不考虑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的方法之一,我们用smoothScrolling列表视图。由于此方法不可用API等级8(升级Froyo)之前,我们使用TargetApi标注为prevent该方法被调用previous SDK版本。

in one of our methods, we use smoothScrolling in a list view. As this method is not available before API Level 8 (FROYO), we used the TargetApi annotation to prevent the method from being called in previous SDK versions.

正如你所看到的,我们的执行的使用TargetApi标注无论是在类的定义和使用的类的对象的语句。这是比需要的。

As you can see, we do use TargetApi annotation both in class definition and in statements that use the objects of the class. This is more than needed.

我们的问题是,TargetApi注释不考虑,使我们的仿真器崩溃的版本ECLAIR(SDK 7)。通过跟踪,我们只是认识到,应当只在版本执行8+的code也执行在第7版。

Our problem is that the TargetApi annotation is not taken into account and make our emulator crash in version ECLAIR (SDK 7). By tracing, we just realize that the code that should only be executed in versions 8+ is also executed in version 7.

我们是否失去了一些东西?

Are we missing something ??

这code是一个监听器:

This code is in a listener :

@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 );
        }
    }
}

和注册该侦听器是这样的:

And the listener is registered this way :

@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() );
    }
}

顺便说一句,我们运行code。在调试模式,所以这个问题是没有关系的混淆删除注释。

BTW, we run the code in debug mode, so the issue is not related to obfuscation removing annotations.

THX提前!

推荐答案

@TargetApi 不prevent从正在运行的任何code,它仅仅是对于标注为新的API code和preventing编译器错误,一旦你知道你只是有条件地调用它们。

@TargetApi does not prevent any code from being run, it is merely for annotating code and preventing compiler errors for new APIs once you know you are only conditionally calling them.

您还需要添加一些东西线沿线的

You still need to add something along the lines of

if (Build.VERSION.SDK_INT > 7){
    //...
}

这篇关于TargetApi不考虑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆