在我們的一種方法中,我們在列表視圖中使用了smoothScrolling。由於此方法在API Level 8(FROYO)之前不可用,因此我們使用TargetApi註釋來防止在以前的SDK版本中調用該方法。TargetApi沒有考慮到
如您所見,我們做在類定義和使用類的對象的語句中都使用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());
}
}
順便說一句,我們運行在調試模式下的代碼,所以這個問題是不相關的混淆刪除註釋。
順便說一句,你可以寫'@TargetApi(Build.VERSION_CODES.FROYO)'而不是'@TargetApi(8)'。 – Wyzard
你說得對。而且你的targetSDK必須儘可能高。 – Snicolas