Visit the Views OnCreate () method report NullPointerException

Source: Internet
Author: User

I followed the tutorial, created a new activity through the wizard, and when I ran the OnCreate method called the Findviewbyid () method times the wrong nullpointerexception.

Layout XML (fragment_main.xml):

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= "PackageName. Mainactivity$placeholderfragment ">

<view
Android:layout_width= "100DP"
android:layout_height= "100DP"
Android:id= "@+id/something"/>

</RelativeLayout>

Processing methods


The tutorial has obviously expired. You should try to create an activity-based active UI instead of fragment based, and your code is obviously based on fragment

Refer to the following code:

1234567891011 Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container,false);View something = rootView.findViewById(R.id.something);// not activity findViewById()something.setOnClickListener(newView.OnClickListener() { ... });returnrootView;}

The Findviewbyid method is in Super.onstart () after OnCreate (). The back executes, they have order.


Original address: http://www.itmmd.com/201410/37.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.

Visit the Views OnCreate () method report NullPointerException

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.