Just learned Android encountered problems, Findviewbyid value is null (new version), The Birds welcome you

Source: Internet
Author: User

Environmental Accountability:

Just learned Android, download the new version of ADT on the official website

and the new SDK

When you create a project in the new version of the IDE (ADT), if your minimum version ( Minimum required SDK ) to support the following version of 4.0, and the target version is (4.0+). Then the IDE creates a Compatibility Pack for you (APPCOMPAT_V7) such as,

After creating the text message project, you will have the following project directory structure

This time in the generated project main activity is not the kind of inherited activity, but the inheritance of the actionbaractivity, I created the text message interface. The interface effect is as follows. Point at this time I use This.findviewbyid (r.id.but_send), to get the texting interface button. But I don't get the button I want, the output button is null.

This makes me a beginner of Android's this whole?? Baidu a HA no available answers, and then solve their own.

Solutions

After a long time to tinker with the success code as follows, because the new layout file is not stored in the default (res/layout/activity_main.xml) file, but is stored in the (res/layout/fragment_main.xml) file. So the ID will be found in the Fragment_main.xml, and the new IDE-generated code loading (fragment_main.xml) file is loaded internally, so we can get the button at the internal class loading point:

 PackageCom.shuqi.cms;Importandroid.support.v7.app.ActionBarActivity;Importandroid.support.v4.app.Fragment;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity{Private StaticEditText Edt_num =NULL; Private StaticEditText edt_content =NULL; Private StaticButton Button =NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    /*** A placeholder fragment containing a simple view. */     Public Static classPlaceholderfragmentextendsFragment {View Rootview=NULL;  Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { Rootview= Inflater.inflate (R.layout.fragment_main, container,false); Edt_num=(EditText) Rootview.findviewbyid (r.id.edt_cms_num); Edt_content=(EditText) Rootview.findviewbyid (r.id.edt_cms_content); Button=(Button) Rootview.findviewbyid (r.id.but_send); //System.out.println (button);                        returnRootview; }            }         Public voidOnClick (View arg0) {if(Edt_num.gettext (). toString (). Trim (). Equals ("") | | Edt_content.gettext (). toString (). Trim (). Equals ("") {Context Context=Getapplicationcontext (); Charsequence text= "Phone number or SMS content cannot be empty!!" "; intDuration =Toast.length_short; Toast Toast=Toast.maketext (context, text, duration);                    Toast.show (); }Else{System.out.println ("Texting"); }    }}

As follows:

other ways to resolve the Findviewbyid value are null:

Original address: http://blog.sina.com.cn/s/blog_5e58565701012q2d.html

ERROR: Findviewbyid returned null, reported nullpointer error

Online search, merely, summary reasons, generally 3 kinds:


1. The element in another view should be taken with Baseview.findviewbyid ()

Findviewbyid () is to specify the view, and if it is not found under the view, it will naturally be null. Usually pay attention to develop the habit of writing View.findviewbyid () is not easy wrong.

2.findViewById in Setcontentview (R.layout.main);

That is, before the Setcontentview call, called Findviewbyid to find the main layout of the interface element Lv_contactbook, then the resulting LV must be null. The right thing to do is to move the line in the above code to the Setcontentview method call.


3.clean Engineering, let ID regenerate

In this case, call Layoutinflater.inflate to convert the contents of the layout XML to the appropriate object. For example, if you have a rowview.xml layout file (such as the layout of the content used as a row in a ListView when customizing adapter), assume that there is code similar to the following in the custom adapter GetView method:
View Rowview = (view) inflater.inflate (R.layout.rowview, parent, false);
TextView tv_contact_id = (TextView) Rowview.findviewbyid (r.id.tv_contact_id);
TextView tv_contactname = (TextView) Rowview.findviewbyid (r.id.tv_contactname);

Sometimes it will also find rowview non-empty, but tv_contact_id and Tv_contactname are null! Look at the code carefully, how can not see the error. What is the cause of it? The answer is eclipse, and to solve this problem, this project will need to be clean once (Project menu, Clean submenu), so it's OK.

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.