I believe many new users, like me, are writing androidProgramAlways encounter this kind of error, and then it is overwhelmed, especially after checking allCodeI still don't know what's going on! Of course, this is related to my insufficient ability to catch errors. Here, I want to sum up several situations that I recently encountered about the nullpointer error (because I am too careless, so there are various types of errors). Alas, new users are always happy! Before the formal conclusion, we must emphasize thatArticleIt's just a record. It does not include all the situations that may cause problems. It's just a record of my situation, so it's not comprehensive. If a reader finds that it is not consistent with his own situation after reading it, sorry, you can tell me your situation and we will discuss it together.
Well, the next step is the text.
first, I must explain a common situation, that is, you must carefully check whether your component has been declared, the program does not find the layout where your component is located. At this time, you should pay attention to whether you have Inflater your layout in the previous Code. If yes, please correctly state that, your ID is on this layout, such as: spinner = (spinner) layout. findviewbyid (R. id. in this way, the program can find your components correctly. Another problem is that you are not careful with programming. For example, you obviously want to put all your components on the layout of your Inflater, you have configured a layout by yourself. However, you will add it before your code or automatically add setcontentview (R. layout. main), this error will occur, because there are two layout, but the main is not used, the error will be reported because the main is empty. I encountered this error in one practice, which really made me feel heartbroken until I was helpless (because I have checked it many times !) Check to find this problem. Another problem is that when you want to add a view to a layout, you must set another layout for the view, because this is actually a layer superposition, but it is very likely that your layout file does not notice this, so it will still report an error. This problem occurs because I am a newbie and try to write the android program from the beginning. As a result, I did not do this. So, I am miserable again ..... (Check the layout file. Later I found that the preview image can be viewed in advance, because the SDK can do this. If the above does not correctly show what should have appeared, your layout file is definitely faulty.)
Then, after you check that all components have declarations and IDs that can be found, there is no problem with layout and the layout file is okay. Then, you need to check your logic, especially loops. An easy problem in a loop is that null assignment may occur. You need to carefully check whether there is null value assignment in your variables, including temporary variables, or if you want to assign values in the loop, however, if you find that you don't need it at all and forget to remove the temporary variable, the problem will occur. This problem is actually because our programming literacy is a little low (yes, it is a good thing to honestly admit that our literacy is low, we may want to assign values to array elements in a loop instead of thinking that we are already very high about what we have done, this process may first declare a temporary variable and then assign it to the array element. In this case, you need to think clearly. If this temporary variable is really a storage space, it makes no sense, it does not need to save the value of the previous loop before each loop starts. Therefore, put it in the loop instead of declaring it outside, because this will happen as I said above, you declare a temporary variable with an empty value assignment. Therefore, it is very important to honestly check the declared variables to see if there will be null values!
The above is what I have encountered. If I encounter other situations or someone tells me about the new situation, I will continue to add this article. Well, let's do it first. I hope I can help new users who are struggling in confusion like me.