EditText NULL pointer

Source: Internet
Author: User

EditText NULL pointer

Today, we have encountered such a problem in Android. In fact, few people may encounter this problem, because it is only such a wonderful thing as I do that.

But now that I have solved the problem, I would like to share it with you here. After all, it has plagued me a day... Let's talk about it more. Let's take a look at the following...

In fact, the problem is very simple, that is, Caused By: java. lang. NullPointerException.

The above is the error type that it prompts me when I use EditText.

For ease, part of the code is truncated as follows, which is the root cause of the problem.

Super. onCreate (savedInstanceState );
Final EditText et = (EditText) findViewById (R. id. editTextId );
String str = et. getText (). toString (). trim ();
SetContentView (R. layout. main );

At first glance, nothing is wrong. This is because I am a newbie, so I didn't see any clues. Then I tried my best to report an error and go online.

This is not the case on the Internet...

The error location shown in the ADT Log is String str = et. getText (). toString (). trim.

But there is nothing wrong with it. You can see that.

Yes, that is, setContentView (R. layout. main); the location of this sentence is wrong. It should be placed before the referenced resource file, where

Final EditText et = (EditText) findViewById (R. id. editTextId. Because the id of R. Id. editTextId

REFERENCE The EditText definition in the Xml file R. layout. main.

Before the final EditText et = (EditText) findViewById (R. id. editTextId) sentence, we didn't put the Xml file R. layout. main

Put the file in our Context. We can observe the findViewById () method and find that the R. id. editTextId in this method is from the current

The Activity is obtained in the xml file of the main layout file. R. layout. main must be declared in the program prior to findViewById (R. id. editTextId.

Then the change is correct:

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );
Final EditText et = (EditText) findViewById (R. id. editTextId );
String str = et. getText (). toString (). trim ();
SetContentView (R. layout. main );

This is my own low-price mistake. It has no reference value for the great gods, but I have been played by it for a while, so it is necessary to share it here.

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.