Android运行时错误

时间:2014-12-23 08:30:55

标签: android

我正在尝试一个示例安卓代码但是当我尝试工作时它会给出致命的错误。 Logcat消息如下:

12-23 10:23:47.436: E/AndroidRuntime(14565): FATAL EXCEPTION: main
12-23 10:23:47.436: E/AndroidRuntime(14565): Process: com.example.proje, PID: 14565
12-23 10:23:47.436: E/AndroidRuntime(14565): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.proje/com.example.proje.ActivityMain}: java.lang.NullPointerException
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.os.Looper.loop(Looper.java:157)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread.main(ActivityThread.java:5356)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at java.lang.reflect.Method.invoke(Method.java:515)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at dalvik.system.NativeStart.main(Native Method)
12-23 10:23:47.436: E/AndroidRuntime(14565): Caused by: java.lang.NullPointerException
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.Activity.findViewById(Activity.java:1970)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at com.example.proje.ActivityMain.<init>(ActivityMain.java:20)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at java.lang.Class.newInstanceImpl(Native Method)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at java.lang.Class.newInstance(Class.java:1208)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
12-23 10:23:47.436: E/AndroidRuntime(14565):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
12-23 10:23:47.436: E/AndroidRuntime(14565):    ... 11 more

1 个答案:

答案 0 :(得分:2)

12-23 10:23:47.436: E/AndroidRuntime(14565): Caused by: java.lang.NullPointerException
12-23 10:23:47.436: E/AndroidRuntime(14565):  at android.app.Activity.findViewById(Activity.java:1970)
12-23 10:23:47.436: E/AndroidRuntime(14565):  at com.example.proje.ActivityMain.<init>

您在对象初始化阶段(例如构造函数或成员变量初始化)过早地调用findViewById()。您无法在findViewById()之前调用onCreate()之类的任何活动方法。将呼叫转移到onCreate()。同样将它放在setContentView()之后,这样你就可以得到一个非空值。