由于上述错误,我无法运行我的应用程序。当我点击“注册”按钮时它崩溃了。在我尝试插入JSON代码(连接到我的数据库)之前,它的行为并不是这样。
我不确定我的JSON代码是否导致任何错误和内容,因此我尝试在此处搜索解决方案,但无济于事。
这是我的logcat的一部分:
05-06 01:11:36.270 1173-1173/com.powerfood2014.app I/Process﹕ Sending signal. PID: 1173 SIG: 9
05-06 01:19:15.030 1227-1227/com.powerfood2014.app D/dalvikvm﹕ GC_FOR_ALLOC freed 74K, 5% free 3029K/3188K, paused 51ms, total 55ms
05-06 01:19:15.040 1227-1227/com.powerfood2014.app I/dalvikvm-heap﹕ Grow heap (frag case) to 3.645MB for 635812-byte allocation
05-06 01:19:15.090 1227-1236/com.powerfood2014.app D/dalvikvm﹕ GC_FOR_ALLOC freed 6K, 5% free 3643K/3812K, paused 49ms, total 49ms
05-06 01:19:15.490 1227-1227/com.powerfood2014.app I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
05-06 01:19:15.540 1227-1227/com.powerfood2014.app D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
05-06 01:19:21.820 1227-1227/com.powerfood2014.app D/AndroidRuntime﹕ Shutting down VM
05-06 01:19:21.820 1227-1227/com.powerfood2014.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ad7ba8)
05-06 01:19:21.860 1227-1227/com.powerfood2014.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.powerfood2014.app, PID: 1227
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.powerfood2014.app/com.powerfood2014.app.Register}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2035)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.powerfood2014.app.Register.<init>(Register.java:162)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2035)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
05-06 01:19:27.660 1227-1227/com.powerfood2014.app I/Process﹕ Sending signal. PID: 1227 SIG: 9
答案 0 :(得分:2)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.powerfood2014.app.Register.<init>(Register.java:162)
初始化成员变量时,您过早地调用findViewById()
- <init>
指的是对象初始化,例如构造函数和成员变量初始化。
将findViewById()
初始化移至onCreate()
,以使其赢得NPE并在setContentView()
之后,以便有机会找到某些内容。
答案 1 :(得分:0)
这可能(可能)不是答案,但要记住一些事情。使用网络连接时应使用Threads
。因此,您会收到此警告:
Skipped 42 frames! The application may be doing too much work on its main thread.
查看Threads
,许多涵盖网络连接的教程(使用JSON)也涵盖Threads
。如果实施得当,Thread
会阻止您的应用程序滞后,没有响应或甚至崩溃。