最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Android项目中@TargetApi语法报错:Cannot resolve symbol TargetApi

Android crifan 666浏览 0评论
折腾:
【未解决】rcsjta项目编译出错:Task tts compileDebugJavaWithJavac TextToSpeech中的speak(String,int,HashMap<String,String>)已过时
期间,代码:
@SuppressWarnings("deprecation")
private void ttsUnder20(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null);
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null, null);
}
语法报错:
Cannot resolve symbol 'TargetApi'
android – TargetAPI – Build cannot be resolved to a variable – Stack Overflow
让直接用Build.VERSION.SDK_INT ,而不要用@TargetAPI
android – error: cannot find symbol Build.VERSION_CODES.KITKAT – Stack Overflow
https://stackoverflow.com/questions/51152394/error-cannot-find-symbol-build-version-codes-kitkat
xml – Why android cannot resolve symbol but it still works? – Stack Overflow
android – Cannot resolve symbol KITKAT Cannot Resolve method getAllocationByteCount() – Stack Overflow
lint – supressLint cannot be resolved to a type error (android beginner tutorial online) – Stack Overflow
加了:
import android.annotation.SuppressLint;
问题依旧。
Cannot resolve symbol  @TargetApi
Java Code Examples for android.annotation.TargetApi
难道是,导入了:android.annotation.TargetApi 就可以了?
import android.annotation.TargetApi;
好像真的可以了,至少不报语法错了:
且Command+鼠标移动上去,可以看到提示:
Android API 23 Platform android.annotation
public @interface TargetApi
点击进去看到
android/annotation/TargetApi.java
/** Indicates that Lint should treat this type as targeting a given API level, no matter what the
    project target is. */
@Target({TYPE, METHOD, CONSTRUCTOR})
@Retention(RetentionPolicy.CLASS)
public @interface TargetApi {
    /**
     * This sets the target api level for the type..
     */
    int value();
}
貌似是可以的
只不过,好像是API 23之后,才有的接口的类
另外想到,此处去看看当前编译的sdk=api版本是否满足最低要求
build.gradle
ext {
    compileSdkVersion = 23
    minSdkVersion = 12
    targetSdkVersion = 23
}
编译版本是23
最小sdk是12
目标sdk是23
算是除了最小sdk,其他都满足了?

另外,注意到之前提示中有:
import class
所以去试试效果:
点击 Import class
也是可以自动找到并导入:
import android.annotation.TargetApi;
的。
和自己(从网上找到相关的类之后,再去)手动导入,是一样的。
-》还是(基于IntelliJ IDEA的)Android Studio足够的智能。

【总结】
此处,对于代码:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null, null);
}
还没编译就会报语法错:
Cannot resolve symbol 'TargetApi'
原因:
Android Studio无法直接和识别此种写法。
解决办法:
导入android.annotation.TargetApi
import android.annotation.TargetApi;
即可。
注:
其实可以通过Android Studio的提示,点击:Import class,自动导入缺失的类的。
后记
已回复帖子
android – TargetAPI – Build cannot be resolved to a variable – Stack Overflow

转载请注明:在路上 » 【已解决】Android项目中@TargetApi语法报错:Cannot resolve symbol TargetApi

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
94 queries in 0.193 seconds, using 23.40MB memory