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 6892157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:29:58+00:00 2026-05-27T06:29:58+00:00

I have a class Drawview: public class DrawView extends View { private ColorBall[] colorballs

  • 0

I have a class Drawview:

public class DrawView extends View {
   private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
   private int balID = 0; // variable to know what ball is being dragged

    public DrawView(Context context) {
        super(context);
        setFocusable(true); //necessary for getting the touch events

        // setting the start point for the balls
        Point point1 = new Point();
        point1.x = 50;
        point1.y = 20;
        Point point2 = new Point();
        point2.x = 100;
        point2.y = 20;
        Point point3 = new Point();
        point3.x = 150;
        point3.y = 20;


        // declare each ball with the ColorBall class
        colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);
        colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);
        colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3);


    }
    }

And my current class is:

public class Quiz1 extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.e);
AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
DrawView d=new DrawView(this);
l.addView(d);
}
}

I am trying to add that DrawView class but its not getting added as a child view of Absolutelayout of my current class view.its executing without any error but i am not able to see Drawview class objects.
And when i am doing this:

 public class Quiz1 extends Activity{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.e);
    AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
    DrawView d=new DrawView(this);
    l.addView(d);
    }
    }

I am getting NullPointerException it means its not renderring the Drawview View.So bottom line is how to add a class extending a View to current view.
Please help me..thanx

  • 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-05-27T06:29:59+00:00Added an answer on May 27, 2026 at 6:29 am

    The view probably is getting added, but from the code that you’ve posted it wouldn’t lay out in a way where anything would be visible. By default, when you add a view to a layout in Java code like you have done, without explicitly setting any LayoutParams, it will set your view to be laid out using wrap_content for both height and width. Since (as far as we can see) you are not overriding any of View’s measurement methods to tell the layout system how big the “content” inside your custom view is, the view will be added to the hierarchy with a height and width of zero.

    Before adding your custom view to the layout, you should add a line to set the layout parameters to fill it’s container (the parent layout), like so:

    AbsoluteLayout l= (AbsoluteLayout)findViewById(R.id.ll);
    DrawView d = new DrawView(this);
    LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0, 0);
    d.setLayoutParams(lp);
    l.addView(d);
    

    Another option is to add your custom view directly to the XML layout R.layout.e, where you can set all these parameters directly in the XML and not worry about doing it in Java code.

    Final side note: AbsoluteLayout has been deprecated for a long time now, and should not be used in new applications. You should use a FrameLayout or RelativeLayout for your application, which provide just as much flexibility.

    HTH

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

Sidebar

Related Questions

I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have this custom class ... public class DrawView extends ImageView When I set
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have 2 classes, Main as main activity and DrawView (extends View) as a
I have a class that subclasses UIView called DrawView. This class contains custom drawing
I have class @Embeddable public class MyClass implements Serializable { private String field1; private
I have Class CustomDate and that is referred in other class called Test. public
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class

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.