Google official notes and google notes
In addition to the popular xutils butterknife annotation framework, google has begun to add its own annotation to support package 19.1, an internal beta version.
Use gradle to add this sentence to your project:
compile 'com.android.support:support-annotations:20.0.0'
Let's take a look at three basic types:
- Nullness
- Resource type
- IntDef and StringDef
Next, let's take a look at how they use Nullness.
@NonNull
Parameter cannot be blank
This Code uses this annotation to pass a null parameter in the past:
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String name = null; sayHello(name); } void sayHello(@NonNull String s) { Toast.makeText(this, "Hello " + s, Toast.LENGTH_LONG).show(); }}
IDE sends a warning
The warning of using non-null parameters disappears.
@Nullable
The parameter or return value cannot be blank.
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); User user = new User("Our Lord Duarte"); Toast.makeText(this, "Hello " + getName(user), Toast.LENGTH_LONG).show(); } @Nullable String getName(@NonNull User user) { return user.getName(); }}
Used@ NullableAnnotation opportunity helps us check
Toast.makeText(this, "Hello " + getName(user), Toast.LENGTH_LONG).show();
If you do not check whether it is null, there will be potential crash problems.
For more details, refer:
Http://anupcowkur.com/posts/a-look-at-android-support-annotations/