Android context Null pointer exception

Source: Internet
Author: User

The code in the fragment context is null

public class Placeholderfragment extends Fragment{context Context = Getactivity ();}

This defines the context as null. Of course, my actual code is not written like this, but this means. It is assumed that the activity will not be empty. In fragment, it is null.

Code context in activity is not NULL

public class Mainactivity extends Fragmentactivity {context context = this;}

The following way to get the context throws an exception

public class Mainactivity extends Fragmentactivity {Context context = getapplicationcontext ();;}

The above is just a matter of defining a single variable, and if the activity you write to the OnCreate method is certainly possible.

Met on the record, we see also will leave a heart.

Android context Null pointer exception

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.