Google official notes and google notes

Source: Internet
Author: User

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.

@NonNullParameter 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.


@NullableThe 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/







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.