Unable to start activity ComponentInfo: java.lang.NullPointerException

柔情痞子 提交于 2019-12-06 07:20:13

We run our app very well and suddenly we encounter NullPointerException or Unable to start activity etc errors.

Basically NullPointerException or Unable to start activity occurs when there is issue in onCreate() method of our Activity.

This occurs when :

  1. We change any xml values of layout related to this Activity

  2. If we do not map xml UI's properly in our Acivity

  3. Try to access UI which is in another layout file.

Solution :

  1. First Cross check all the mapped elements

  2. Give unique naming

Directly after:
TextView txt=(TextView)findViewById(R.id.xtra);

... add this:
if (txt == null) { Log.w("", "TextView is null"); }

Assuming your null pointer exception doesn't occur until you select the checkbox, that sounds like the most likely issue. I've encountered the same when I forget that I removed the corresponding element from the XML layout, or if I got the ID wrong. Usually I wrap any actions upon an element returned by "findViewById" within a null check, to ensure that even if the retrieval fails, the app at least won't crash.

Looks like you're assigning to chk, and subsequently, txt by calling findViewById as you declare them. I had to declare them first, and then assign to them using findViewById.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!