My code is giving me a NullPointerException and I’ve read through posts suggesting a clean-and-rebuild but that approach hasn’t helped me so far.
My stack trace is as follows:
E/AndroidRuntime(553): FATAL EXCEPTION: main
E/AndroidRuntime(553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.touchlogger/com.example.touchlogger.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(553): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(553): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(553): Caused by: java.lang.NullPointerException
E/AndroidRuntime(553): at android.view.ViewGroup.addViewInner(ViewGroup.java:3336)
E/AndroidRuntime(553): at android.view.ViewGroup.addView(ViewGroup.java:3208)
E/AndroidRuntime(553): at android.view.ViewGroup.addView(ViewGroup.java:3188)
E/AndroidRuntime(553): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
E/AndroidRuntime(553): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
E/AndroidRuntime(553): at android.app.Activity.setContentView(Activity.java:1855)
E/AndroidRuntime(553): at com.example.touchlogger.MainActivity.onCreate(MainActivity.java:30)
E/AndroidRuntime(553): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(553): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
My XML file is as follows:
<com.example.touchlogger.DrawView
android:id="@+id/drawView1"
android:layout_width="wrap_content"
android:layout_height="357dp" />
<Button
android:id="@+id/button1"
android:layout_width="160dp"
android:layout_height="49dp"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="29dp"
android:text="End" />
</LinearLayout>
and the line which is causing the error is:
setContentView((LinearLayout)(findViewById(R.id.layout)));
I’m new to Android programming and even if the problem is quite simple, I’m afraid it just doesn’t make any sense to me at all. I’ve spent over two days on this and I’m completely and utterly lost. My code was working up until I decided to include a button with the drawView. That’s where it got a little screwed up. I’ve only been having problems ever since.
Any help would be hugely appreciated! Thanks in advance!
It won’t work because your are calling the
findViewById()method before thesetContentView()one, so it will search for a view that it hasn’t created yet. Use this instead :