Solution: keeps stopping (stop running)

Today when I was writing Android, I had to write a thread. Normally it should be like this:

Insert picture description here

However, after typing the code, run ...

Insert picture description here

  • For this problem, the Internet is very complicated, and finally I have to ask the teacher, and finally, solve it. The steps are shared as follows:
  1. First of all, 赶紧看一下调试神器Logcatis it red? Congratulations, the problem is in front of you
    Insert picture description here

  2. Look at the error: "java.lang.RuntimeException: Unable to start activity ComponentInfo {cn.edu.jssvc.simpleview / cn.edu.jssvc.simpleview.MainActivity}: java.lang.NullPointerException"
    from the second line of the log It can be seen that the error appears in MainActivitythis class, which is aNullPointerException

  3. Then find the error code, then log down, you can see (MainActivity.java:37), click this blue word, the cursor will jump to the error code, MainActivity line 37, as shown in the figure On line 37, the error reported is NullPointerExceptionthat there is no pointer to it, generally 变量没有赋值或者没有正确赋值,但是可以看到第30行,已经给button赋值了,所以只能是另一个原因,没有正确赋值.
    Insert picture description here

Move the mouse to the button, press and hold the Ctrl key on the keyboard, you can see that this R.id.button is from activity_main.xml,
Insert picture description here

On the 28th line of the code, the loaded layout is activity_simpleview.xml, which does not have this id.

setContentView(R.layout.activity_simpleview);
  1. Finally, correct the error and change the R.id.button here to the Button id in activity_simpleview.xml.
    Of course, this is just an example, 如果对错误还是没有头绪或者没有解决问题,再回来仔细看一下Logcat,还是不知道怎么改,百度一下错误类型,网络上基本可以解决90%的问题if you still can't solve it, please leave a message, or add our QQ group: 849113536.

Reprinted: Solved: keeps stopping (stop running)

Published 51 original articles · Like 48 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/m0_46153949/article/details/105680152