java.lang.RuntimeException: Unable to start activity ComponentInfo{包名/类名}

写这篇博文,我顶着很大的压力,贴出来会引来网友的一片鄙视,不贴我又觉得对不起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;
    }
这得害了多少无辜的程序员。

你可能感兴趣的:(java.lang.RuntimeException: Unable to start activity ComponentInfo{包名/类名})