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.