FATAL EXCEPTION: ……

  java.lang.RuntimeException: Unable to start activity ComponentInfo……

  ava.lang.NullPointerException: Attempt to invoke virtual method 'android……on a null object reference

介绍:

  我在写项目的时候,遇到了button等控件出现on a null object reference ,在网上找了很久没找到解决办法。最后还是自己发现了问题的来源,又自己改正了。具体形式如上。

问题出现的情况:

  布局有该,并且在java代码中也有 findViewById语句, 但是调试的时候还是出了on a null object reference错误。

  

问题出现的原因:

  在安卓开发的时候,遇到类似的布局以及实现的方法的时候,我们就习惯直接复制。

  一般来说,不同布局中设置id为同名的时候,是不会报错的。但是复制的情况特殊,虽然把setsetContentView(R.layout.xxx)中xxx的名字修改了,但是findViewById中的ID却没有改变。也就是说还是源布局(也就是被复制的那个布局)里的ID。

  有什么证据呢?

  鼠标左键停止findViewById(R.id.yyy)中的yyy上,比较复制的java文件和源java文件,会发现他们的ID是一样的。

解决办法:

  原因找到了,解决办法就呼之欲出了。

  我的做法是把第二个布局里的控件名字全改了(加了个前缀)。这个时候,会发现java代码不会报错。但是findViewById里的内容也要改。

  感觉这样改正还是挺麻烦的,期待有更好的办法。

on a null object reference 问题的解决办法的更多相关文章

  1. java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.xxx.Json.NewsBean.getError_code()' on a null object reference错误解决

    AS在运行的过程中出现了错误: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.xx ...

  2. Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

    E/AndroidRuntime﹕ FATAL EXCEPTION: mainProcess: org.example.magnusluca.drawertestapp, PID: 3624java. ...

  3. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference

    /********************************************************************************* * Caused by: java ...

  4. java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.yunweather.app.db.YunWeatherDB.loadProvinces()' on a null object reference

    NullPointerException:查看自己的什么地方是否对空指针进行了操作 Attempt to invoke virtual method 'java.util.List com.yunwe ...

  5. android.widget.TextView.setText() on a null object reference

    错误描述 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView. ...

  6. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  7. wince6.0 编译报错:"error C2220: warning treated as error - no 'object' file generated"的解决办法

    内容提要:wince6.0编译报错:"error C2220: warning treated as error - no 'object' file generated" 原因是 ...

  8. docker学习之路-build asp.net core 2.2产生 warning MSB3245: Could not resolve this reference.错误的解决办法

    在docker build的时候有时我们可以直接使用dotnet publish来发布,但是如果用docker构建镜像的时候却会出现下面的错误: 解决办法:https://stackoverflow. ...

  9. 使用eval将字符串转化成字典时报name 'null' is not defined错误解决办法

    在接口测试过程中,为了取值将形如字典形式的字符串使用eval()方法转化成字典方便取值 str={"code":100,"num":1,"data&q ...

随机推荐

  1. Android 系统广播机制

    一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...

  2. linked-list-cycle——链表、判断是否循环链表、快慢指针

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. MySql视频教程——百度云下载路径

    百度云分享MySql视频教程给大家.祝大家事业进步! MySql视频教程:http://pan.baidu.com/s/1gdCHX79 password:n46i

  4. centos下的hadoop服务器的配置

    是我安装CentOS服务器的过程,记录下来,与大家一起分享. 安装操作系统 CentOS 6.2 ,CentOS-6.2-i386-bin-DVD1.iso(32位) ,CentOS-6.2-x86_ ...

  5. 理解OpenStack认证:Keystone PKI

    原文链接: https://www.mirantis.com/blog/understanding-openstack-authentication-keystone-pki/ The latest ...

  6. UnsatisfiedLinkError: No implementation found for , AndroidStudio使用*.so

    今天工作的时候.发现了一个jni的问题,java.lang.UnsatisfiedLinkError: No implementation found for...... 问题1:后来查了资料后发现. ...

  7. JSP与HTML的差别

    HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性 语言呢?由于.仅仅要你有一个浏览器那么它就能够正常显示 ...

  8. LeetCode(66)题解: Plus One

    https://leetcode.com/problems/plus-one/ 题目: Given a non-negative number represented as an array of d ...

  9. Android实现RecyclerView的下拉刷新和上拉载入很多其它

    需求 先上效果图, Material Design风格的下拉刷新和上拉载入很多其它. 源代码地址(欢迎star) https://github.com/studychen/SeeNewsV2 假设对于 ...

  10. IOS中调用系统拨打电话发送短信

    一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...