Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:49:25+00:00 2026-06-14T03:49:25+00:00

My code is giving me a NullPointerException and I’ve read through posts suggesting a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:49:26+00:00Added an answer on June 14, 2026 at 3:49 am

    It won’t work because your are calling the findViewById() method before the setContentView() one, so it will search for a view that it hasn’t created yet. Use this instead :

    setContentView(R.layout.my_xml_file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code that is giving me trouble. I know all
i am using javax.Mail api with authenticator, but this below code giving Access restriction:
The following code is giving me a NullPointerException . The problem is on the
I'm getting an error that says java.lang.NullPointerException: null from a code whenever I run
The following code is giving me this error: java.lang.NullPointerException java.io.FileOutputStream.(Unknown Source) java.io.FileOutputStream.(Unknown Source) java.io.FileWriter.(Unknown
i have a code giving me possible combinations, but its missing the combinations of
Why is the following code giving an exception? function SetText(index, text) { try {
This code is giving me this error, which I don't understand. I can't even
For some reason this line of code is giving me quite a problem. struct
I've simplified my JavaScript code to the example below; this code is giving me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.