The findViewById value is null (new version) when I first learned about Android. Thank you for your correction.

Source: Internet
Author: User

Environment Description:

I just learned about Android and downloaded the new version of ADT from the official website.

And new sdks

When creating a project in the new IDE version (ADT), if your minimum version (minimum required SDK) must support versions earlier than 4.0, and the target version is (4.0 + ). then, IDE will create a compatible package for you (appcompat_v7), for example,

 

After a text message project is created, the following project directory structure will be created:

 

 

At this time, the generated project main Activity is not the previous inherited Activity, but the inherited ActionBarActivity. I created the text message interface. The interface effect is as follows. Click this. findViewById (R. id. but_send); to get the Button on the SMS page. The output button is null.

 

What makes android a beginner ?? Baidu yiha has no answer available, so he solves it by himself.

Solution

After a long time, the Code is as follows, because the new layout file is not stored in the default (res/layout/activity_main.xml) file, but in (res/layout/fragment_main.xml) file. Therefore, you need to find the corresponding ID at the beginning of fragment_main.xml, while the new code generated by the IDE loads the (fragment_main.xml) file internally, so we can get the Button at the internal class loading:

Package com. shuqi. cms; import android. support. v7.app. actionBarActivity; import android. support. v4.app. fragment; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. B Utton; import android. widget. editText; import android. widget. toast; public class MainActivity extends ActionBarActivity {private static EditText edt_num = null; private static EditText edt_content = null; private static Button button = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); if (savedInstanceState = Null) {getSupportFragmentManager (). beginTransaction (). add (R. id. container, new PlaceholderFragment ()). commit () ;}@ Override public boolean onCreateOptionsMenu (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 boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item Clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);}/*** A placeholder fragment containing a simple view. */public static class PlaceholderFragment extends Fragment {View ro OtView = null; public PlaceholderFragment () {}@ Override public View 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); return rootView;} public void onClick (View arg0) {if (edt_num.getText (). toString (). trim (). equals ("") | edt_content.getText (). toString (). trim (). equals ("") {Context context = getApplicationContext (); CharSequence text = "the phone number or text message content cannot be blank !! "; Int duration = Toast. LENGTH_SHORT; Toast toast = Toast. makeText (context, text, duration); toast. show ();} else {System. out. println ("text message ");}}}

As follows:

Other methods to solve the problem where the findViewById value is null:

Address: http://blog.sina.com.cn/s/blog_5e58565701012q2d.html

Error: findViewById returns Null and the nullpointer error is returned.

After searching on the internet, I found three reasons:


1. Use baseView. findViewById () to obtain the element of another view.

FindViewById () specifies the view. If the view cannot be found, null is reported. Note that the habit of writing view. findViewById () is not easy or wrong.

 

2. findViewById before setContentView (R. layout. main.

That is, before setContentView is called, if findViewById is called to find the Interface Element lv_contactbook in the main layout, the obtained lv must be null. The correct method is to move the rough line in the above Code to the setContentView method call.


3. clean the project and re-generate the ID

In this case, call LayoutInflater. inflate to convert the content specified in the layout xml to the corresponding object. For example, a rowview. xml layout file (for example, used as the layout of a row in the ListView when a custom Adapter is used) is provided. It is assumed that the getView method of the custom Adapter has the following code:
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 we can find that rowview is not empty, but both TV _contact_id and TV _contactname are null! I can't see any errors even though I look at the code carefully. What is the cause? The answer is caused by Eclipse. To solve this problem, you need to clean the Project once (Project menu-> Clean sub-menu), so that it is OK.

 

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.