Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android app keeps crashing when I make use of the findViewByID method

This is the information I got from the logcat in android studio after the crash. I specifically indented the line that pointed to an error been caused by the use of the findViewByID method.

--------- beginning of crash 2019-08-25 18:07:18.213 8414-8414/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.numberguessinggame, PID: 8414 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.numberguessinggame/com.example.numberguessinggame.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249) at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182) at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191) at com.example.numberguessinggame.MainActivity.<init>(MainActivity.java:18) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  2019-08-25 18:07:18.245 8414-8414/? I/Process: Sending signal. PID: 8414 SIG: 9

1 Answer

Matthew Timmons
Matthew Timmons
33,804 Points

Hi Ibrahim, if possible, can you include some of your code in your question? It is hard to tell where the error occurs without knowing more about the context.

Regardless, I googled the error you got and found a good Stack Overflow article: https://stackoverflow.com/questions/36666091/attempt-to-invoke-virtual-method-android-view-windowcallback-android-view-wind

I think you may likely be calling findViewById inside of a lifecycle hook that occurs before the page contents are generated. That may be why it says there is a null object reference. I think the fix would be to move your findViewById call into onCreate().

Please let me know if you would like any more help. Thanks!