如何在Log Cat中查找错误和行号

时间:2014-10-27 11:52:03

标签: java android android-layout android-intent

我正在开发一个Android应用程序,但是当我尝试在模拟器上运行它时,我遇到了问题。 我从这个网站上读了几个答案但是我自己无法解决问题。我的logcat显示错误,但我无法找到它是什么错误以及发生了什么。

02-27 17:30:34.991: D/AndroidRuntime(2997): Shutting down VM
02-27 17:30:35.011: W/dalvikvm(2997): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
02-27 17:30:35.061: E/AndroidRuntime(2997): FATAL EXCEPTION: main
02-27 17:30:35.061: E/AndroidRuntime(2997): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mjcet/com.example.mjcet.MainActivity}: java.lang.NullPointerException
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.os.Looper.loop(Looper.java:137)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at java.lang.reflect.Method.invoke(Method.java:525)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at dalvik.system.NativeStart.main(Native Method)
02-27 17:30:35.061: E/AndroidRuntime(2997): Caused by: java.lang.NullPointerException
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.Activity.findViewById(Activity.java:1853)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at com.example.mjcet.MainActivity.<init>(MainActivity.java:14)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at java.lang.Class.newInstanceImpl(Native Method)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at java.lang.Class.newInstance(Class.java:1130)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
02-27 17:30:35.061: E/AndroidRuntime(2997):     ... 11 more

2 个答案:

答案 0 :(得分:1)

引起:java.lang.NullPointerException

02-27 17:30:35.061: E/AndroidRuntime(2997): at android.app.Activity.findViewById(Activity.java:1853)
02-27 17:30:35.061: E/AndroidRuntime(2997):at com.example.mjcet.MainActivity.<init>(MainActivity.java:14)

从这里,您将了解class中的错误行号

答案 1 :(得分:0)

    Caused by: java.lang.NullPointerException
02-27 17:30:35.061: E/AndroidRuntime(2997):     at android.app.Activity.findViewById(Activity.java:1853)
02-27 17:30:35.061: E/AndroidRuntime(2997):     at com.example.mjcet.MainActivity.<init>(MainActivity.java:14)

问题在于:

at com.example.mjcet.MainActivity.<init>(MainActivity.java:14)

MainActivity,第14行,空指针异常

您尝试查找的视图很可能是null(在调用findViewById之前,不在活动的xml中或调用onCreate。)。