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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:55:06+00:00 2026-05-21T22:55:06+00:00

I want to sketch a graph that track the user weight progress, I am

  • 0

I want to sketch a graph that track the user weight progress,
I am writing a method that I can call in the onCreat event,
this is my code:

 public void drawGraph(){
    Display display = getWindowManager().getDefaultDisplay();   
private int width = display.getWidth();  
private int height = display.getHeight();                                               
    Paint paint = new Paint();
    Canvas canvas =new Canvas();
    paint.setColor(Color.GRAY);
    canvas.drawLine(0, 0, 0, height, paint); // the x-axes represent the date        
    canvas.drawLine(0, height, width, height, paint); // the y-axes represent the weight  

    paint.setColor(Color.RED);
    canvas.drawLine(0, max, width, max, paint); //draw the maximum healthy weight
    paint.setColor(Color.YELLOW);
    canvas.drawLine(0, min, width, min, paint); // draw the minimum healthy weight
    paint.setColor(Color.GREEN);
    canvas.drawLine(0, gW, width, gW, paint); // draw the goal weight
            int xDis = width/weekNumbers;
            int y;
    Path path = new Path();     
    for (int i = 0; i <= Weightvalues; i++){
        Cursor c = db.rawQuery("SELECT CURRENTWEIGHT FROM WEIGHT WHERE DATECREATED > " + startDate, null);
        int weight;
        if (c!=null){
            if (c.moveToFirst()){
                do{
                    weight = c.getInt(0);
                    // I want now to find out for each entry the point represent it on the graph
                    y = Math.round(weight * y / range); //range is the difference between the maximum weight and the minimum weight
                    if (i==1)
                        path.moveTo(0, y);
                    else
                        path.lineTo(0 + i*xDis, height-y);

                }while(c.moveToNext());
            }
        }
    }
    paint.setColor(Color.BLUE);
    canvas.drawPath(path, paint);

}  

this is the onCreat event

protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.weight_chart);
      helper = new DataBaseHelper(this);
      drawGraph();
}

when i run the program i faced this error

    ERROR/AndroidRuntime(738): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*********.WeightChart}: java.lang.NullPointerException

Can any body review it for me and tell me where i went wrong

Best regards

  • 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-21T22:55:07+00:00Added an answer on May 21, 2026 at 10:55 pm

    You seem to use Canvas without assigning it a Bitmap to do drawing onto. To get your image on screen, one way is to define ImageView in your layout xml;

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/graph_image" />
    

    Accompanied with following changes to your drawGraph method;

    ...
    Bitmap bitmap = BitmapCreateBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas = new Canvas(bitmap);
    ...
    Do your drawing stuff..
    ...
    ImageView iv = (ImageView)findViewById(R.id.graph_image);
    iv.setImageBitmap(bitmap);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want build a sketch pad app on iPhone, I assume that this type
I want to build a user interface similar to the sketch below: When the
In short, this is a sketch of the JSON object I want to parse
I want to create a website that allows me to sketch something in real
I have a sketch that I want to put up on my website, and
Hallo, I writing an app and I want to integrate a sketch-app to draw
Want to do this: (EDIT: bad sample code, ignore and skip below) struct RECORD
want to have a Hyperlink-Button in a gridView in which I can display a
Users can sketch in my app using a very simple tool (move mouse while
I want to know if there are existing technology that make your 3d models

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.