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

  • SEARCH
  • Home
  • 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 8915163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:54:50+00:00 2026-06-15T04:54:50+00:00

I have a custom view that I declare in ‘main’ XML File: <com.app.DrawView android:id=@+id/my_view

  • 0

I have a custom view that I declare in ‘main’ XML File:

    <com.app.DrawView
    android:id="@+id/my_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

Then in my activity’s onCreate method:

setContentView(R.layout.main);

Which all works fine for displaying the custom view. But what I need to do is pass values to the custom view. So, I tried making an instance of the custom view class:

DrawView drawView = (DrawView) findViewById(R.id.my_view);

Then, in the DrawView class, I created a method:

public void setUserData(boolean data){
      useEraser = data;
}

Now, what my problem is I keep getting a NullPointerExeption when trying to access this method for the DrawView object within my activity, like so:

boolean value = true;
drawView.setUserData(value);

The error points to the “drawView.setUserData(value)” line. What am I doing wrong? I know I must have made a simple mistake but its driving me crazy trying to find it. Any help or suggestions would be great!

EDIT: I accepted an answer which worked correctly, however, I still wanted to know why my original solution did not work. So, I found that, despite the fact that I ordered it correctly in the question, I placed

DrawView drawView = (DrawView)findViewById(R.id.my_view);

before

setContentView(R.layout.main);

I knew it was a simple mistake I was overlooking! Thanks for the answers everyone I appreciate it. And I hope this proves to be useful for anyone faced with a similar problem.

  • 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-15T04:54:51+00:00Added an answer on June 15, 2026 at 4:54 am

    if you want to create new one try this,

    put an linearlayout in your xml as a holder

    <LinearLayout
            android:id="@+id/ln1"
            android:layout_width="wrap_content"
           android:layout_height="300dp" >
    
        </LinearLayout>
    

    and in oncreate use below

    LinearLayout ln = (LinearLayout)findViewById(R.id.ln1);
             com.example.mixed.DrawView dw2 = new DrawView(getApplicationContext());
             dw2.setUserData(false);
             ln.addView(dw2);
    

    it works there are no problem if you put it in your layout xml.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view in my Android App that uses a ScaleGestureDetector.SimpleOnScaleGestureListener to
In the app, I'm struggling with I have a custom view. I cannot declare
I have a custom view that looks a little like a speedometer. <?xml version=1.0
I have a custom View that I would like to embed in an WebView.
I have a custom view that accepts drags. In the custom view there is
I'm using Drupal 6. I have a custom view that needs to be filtered
I have a custom control/view that observes the direction of a gesture within its
I have a custom-made view that extends the View class. I would like 2
I have a simple custom view that is connected via outlet to a NIB.
I have a custom list view that is getting data from server and changing

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.