为什么NullPointer异常

时间:2013-04-03 05:05:43

标签: android

我在开发过程中遇到过这个问题,这个类已经注册了androidManifest,但是仍然有这个问题,在我确认后使用androidManifest包名是正确的

Error log:Unable to start activity ComponentInfo{com.cs.flyox/com.cs.flyox.activity.SelectFormWorkActivity}: java.lang.NullPointerException
'2013-04-02 19:28:33    
Error log:android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
'2013-04-02 19:28:33    
Error log:android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
'2013-04-02 19:28:33    
Error log:android.app.ActivityThread.access$600(ActivityThread.java:128)
'2013-04-02 19:28:33    
Error log:android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
'2013-04-02 19:28:33    
Error log:android.os.Handler.dispatchMessage(Handler.java:99)
'2013-04-02 19:28:33    
Error log:android.os.Looper.loop(Looper.java:137)
'2013-04-02 19:28:33    
Error log:android.app.ActivityThread.main(ActivityThread.java:4517)
'2013-04-02 19:28:33    
Error log:java.lang.reflect.Method.invokeNative(Native Method)
'2013-04-02 19:28:33    
Error log:java.lang.reflect.Method.invoke(Method.java:511)
'2013-04-02 19:28:33    
Error log:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
'2013-04-02 19:28:33    
Error log:com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
'2013-04-02 19:28:33    
Error log:dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在您的活动的onCreate()方法中,您正在进行一个返回空指针的调用。例如,在我的活动中,我做了一个这样的创建方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.landing);
    String pig = savedCueCardActivity.getAction();
}

它创建了一个这样的调试输出:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raac.student/com.raac.student.LandingActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

因为这一行:

String pig = savedCueCardActivity.getAction();

返回null。你的onCreate中有类似的东西,你可以通过在那里设置断点来找到它,或者甚至可以更仔细地查看你的完整调试器输出。