Nic*_*Eye 26 android annotations casting suppress android-support-library
其中一种情况是从Bundle中读取一个int并将其存储到由@IndDef注释限制的变量中:
public class MainActivity extends ActionBarActivity {
@IntDef({STATE_IDLE, STATE_PLAYING, STATE_RECORDING})
@Retention(RetentionPolicy.SOURCE)
public @interface State {}
public static final int STATE_IDLE = 0;
public static final int STATE_PLAYING = 1;
public static final int STATE_RECORDING = 2;
@MainActivity.State int fPlayerState = STATE_IDLE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
fPlayerState = savedInstanceState.getInt(BUNDLE_STATE); //Causes "Must be one of: ..." error
Run Code Online (Sandbox Code Playgroud)
必须有某种方法可以抑制检查或从int转换为@ MainActivity.State int,以便在最后一行设置变量.
另一种情况是编写一个负面测试,调用带有注释参数的函数故意传递错误的参数,以便测试在这种情况下抛出异常.必须有一种方法来抑制注释检查以编译这样的测试.
Nic*_*Eye 52
我找到了抑制注释检查的方法.实际上,有三个:
@SuppressWarnings("ResourceType")在课程定义之前添加.就我而言:
@SuppressWarnings("ResourceType")
public class MainActivity extends ActionBarActivity {
...
}
Run Code Online (Sandbox Code Playgroud)@SuppressWarnings("ResourceType")在方法定义之前添加.就我而言:
@Override
@SuppressWarnings("ResourceType")
protected void onCreate(Bundle savedInstanceState) {
...
}
Run Code Online (Sandbox Code Playgroud)这两种方法对我不起作用,因为我希望对所有代码进行注释检查,除了一个语句.
要禁止检查单行,请添加特殊格式的注释(!!!).
//noinspection ResourceType
fState = savedInstanceState.getInt(BUNDLE_STATE);
Run Code Online (Sandbox Code Playgroud)@Status int state1=bundle.getInt(STATE_ELEMENT1);
setStatus1(state1);
//instead of direct setStatus1(bundle.getInt(STATE_ELEMENT1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6307 次 |
| 最近记录: |