写这篇博文,我顶着很大的压力,贴出来会引来网友的一片鄙视,不贴我又觉得对不起Android SDK研发团队。
本着对全世界Android无产者负责的态度,今天不得不指出Android编译时隐藏的很深的一个Bug.
 
以下为我的TestActivity类:
public class TestActivity extends Activity {
    private static final String TAG = "TestActivity";
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
 
       button = (Button)findViewById(R.id.btn);
       button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
               Log.d(TAG, "onClick. ");
          }
     });
    }
}
 
该类在AndroidMenifest.xml中是这样设置的:
                  android:label="@string/app_name">
            
                
                
            
 
布局文件为res/layout/main.xml:
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    android:name="@+id/btn" android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:text="start"/>
 
很简单吧,当我编译时,也完全正常.但是当在Eclipse中点击运行时,抛出如下异常:
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orange.test/com.orange.test.TestActivity}: java.lang.NullPointerException
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4627)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at com.orange.test.TestActivity.onCreate(TestActivity.java:24)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
     ... 11 more
 
很是匪夷所思,这么一个简单的问题,button=(Button)findViewById(R.id.btn);
button对象值竟然为null,我又去查看gen/目录下的com.orange.test.R.java文件:内容如下
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int btn=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}
 
一切都正常啊,R.id.btn这不是正常地生成了吗!!!!,找来大拿,不顶用,同样没有发现。
最后无意识地发现,android:name="@+id/btn" 应该是android:id="@+id/btn".
悲剧的是,Android编译时,竟然没有发现这里的错误。
 最为可气的是,竟然还在R.java文件中生成了
 public static final class id {
        public static final int btn=0x7f050000;
    }
这得害了多少无辜的程序员。

 

bug_ _java.lang.RuntimeException: Unable to start activity ComponentInfo{包名/类名}的更多相关文章

  1. 转载java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinner/com.example.spinner.MainActivity}: java.lang.NullPointerException

    今天学习Android开发突然遇到了这个问题,查阅了很多资料,并且对集中原因进行了分析. 错误信息字符串:java.lang.RuntimeException: Unable to start act ...

  2. Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

    如果你更新了ADT的新版本,而工程文件中使用了其他的jar包,也可能会出现"java.lang.RuntimeException: Unable to instantiate activit ...

  3. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常总结

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常总结 做android开发的可能都碰到"j ...

  4. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.L ...

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

    异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.Result ...

  6. java.lang.RuntimeException: Unable to start activity ComponentInfo……AppCompat does not support the current theme features

    Android测试时出现闪退的问题,出现了如下所示异常: java.lang.RuntimeException: Unable to start activity ComponentInfo{thon ...

  7. Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}

    Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi ...

  8. android studio调试报错:java.lang.RuntimeException: Unable to start activity ComponentInfo

    报错信息: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pro_u_loc/com.e ...

  9. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.love5/com.example.love5.Main11Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.

    若有 java.lang.RuntimeException和 java.lang.NullPointerException: Attempt to invoke virtual method 'voi ...

随机推荐

  1. Java——线程间通信问题

     wait和sleep区别: 1.wait可以指定时间可以不指定.     sleep必须指定时间. 2.在同步时,对cpu的执行权和锁的处理不同.     wait:释放执行权,释放锁.     ...

  2. 在IE地址栏输入JS的有趣效果

    1.编辑网页 在地址栏输入下面的代码按enter,网页上所有元素都能变成可编辑状态,你可以移动.调整元素大小.如果你只是讨厌某个网站想发泄一下,我建议你使用NetDisater. 代码如下: java ...

  3. 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach

    From Facial Parts Responses to Face Detection: A Deep Learning Approach ICCV 2015 从以上两张图就可以感受到本文所提方法 ...

  4. lua 初接触 --- The first time use Lua for programing

    The first time use Lua for programing Wang Xiao 1. 关于 lua 的变量类型:  lua 变量的定义与matlab有点不同: local d , f ...

  5. php安全的改进以及检测文件是否被篡改

    可以使用svn的check for modifications//查看更改 至于缓存的php模板文件 可以查找以下执行命令的函数是否存在phpinfo,eval,exec,passthru,shell ...

  6. shell之数组

    1.从数组的下标分为索引数组.关联数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /* 索引数组,即通常情况下所说的数组 */ var ary1 = [1,3,5, ...

  7. linux服务之audit

    http://blog.chinaunix.net/uid-20786165-id-3167391.html http://blog.chinaunix.net/uid-8389195-id-1741 ...

  8. JS刷新页面的几种方法(转)

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  9. oracle11gRAC环境使用RMAN增量备份方案

    转摘:http://blog.itpub.net/29819001/viewspace-1320977/ [oracle@zx ~]$ rman target /Recovery Manager: R ...

  10. 【linux】chmod命令详细用法

    功能说明: 变更文件或目录的权限 u:User,即文件或目录的拥有者. g:Group,即文件或目录的所属群组. o:Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围. a:Al ...