写这篇博文,我顶着很大的压力,贴出来会引来网友的一片鄙视,不贴我又觉得对不起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. 越狱Season 1-Episode 19: The Key

    Season 1, Episode 19: The Key -Kellerman: WeusedtohaveaGreatDane, Dane: 丹麦大狗 我们以前有一只大丹犬 bigandwild. ...

  2. 用Spring的mappingDirectoryLocations来配置Hibernate映射文件

    在Spring的applicationContext.xml中配置映射文件的方法: <property name="mappingResources">     < ...

  3. understanding checkpoint_completion_target

    Starting new blog series – explanation of various configuration parameters. I will of course follow ...

  4. 怎么给OCR文字识别软件设置正确的扫描分辨率

    ABBYY FineReader 12是一款专业的OCR文字识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,不仅支持对页扫描,还支持多页扫描,扫描分辨率的选 ...

  5. MyEclipse设置内存

    Tomcat直接启动正常,通过myeclipse启动tomcat内存溢出. MyEclipse启动Tomcat无视catalina.bat中设置内存大小的问题. 在 tomcat的catalina.b ...

  6. 【转】WinForm不同版本覆盖安装

    vs2005为winform程序做的安装包.在以有程序旧版本的机子上用新版本的安装包安装软件时提示 “以经安装该产品的另一个版本.无法继续安装此版本........” 在安装部署项目中设“Remove ...

  7. SMO序列最小最优化算法

    SMO例子: 1 from numpy import * 2 import matplotlib 3 import matplotlib.pyplot as plt 4 5 def loadDataS ...

  8. javascript中的removeEventListener失效问题

    一般现在我们用js绑定事件是 addEventListener(event,function,bool) event:事件的名称 字符串 如:'click', function: 事件处理的方法: b ...

  9. 【shell】变量的配置文件

    (1)/etc/profile   登录时,会执行.全局(公有)配置,不管是哪个用户,登录时都会读取该文件.(2)/ect/bashrc   Ubuntu没有此文件,与之对应的是/ect/bash.b ...

  10. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...