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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:07:33+00:00 2026-06-04T23:07:33+00:00

I have to use a canvas class to add frame and multiple images on

  • 0

I have to use a canvas class to add frame and multiple images on it. but my problem is that I have to use canvas view in my activity class. I want to use this canvas view as a widget so I can use it in my XML.

I tried this by using this code :

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class TwoDee extends View {
    private int mWidth;
    private int mHeight;


    public TwoDee(Context context) {
        super(context);
    }

    public TwoDee(Context context, AttributeSet attribs) {
        super(context, attribs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint(); 
        paint.setColor(Color.GRAY); 
        paint.setStyle(Style.FILL); 
        canvas.drawPaint(paint);

        paint.setColor(Color.BLUE);
        canvas.drawLine(0, 0, mWidth, mHeight, paint);
        canvas.drawLine(mWidth, 0, 0, mHeight, paint);

        paint.setColor(Color.RED);
        canvas.drawText("0 kal", 50, 85, paint);
        canvas.save();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    public void setImage(Drawable myImg) {
        newImg = myImg;
        invalidate();
    }
}

and used it in my xml class like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
        />
        <com.yourapphere.TwoDee
            android:id="@+id/twoDee1"
            android:layout_width="150dp"
            android:layout_height="100dp"
        />
</LinearLayout>

and I am accessing it in my activity class like this :

TwoDee myCanvas;
myCanvas = (TwoDee)findViewById(R.id.twoDee1);
myCanvas.setImage(drawable);

my problem is that when I try to call setImage() function from my activity class it shows null pointer exception. What should I do to add image in canvas view using my Activity class.
I dont want to use setContentView because this will make only Canvas View to show in my activity.

LogCat Output :

FATAL EXCEPTION: main
java.lang.NullPointerException
at iTagz.android.Dialog_ImagePreview$1.onClick(Dialog_ImagePreview.java:84)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9081)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
at dalvik.system.NativeStart.main(Native Method)

Dialog_ImagePreview is my activity from where I’m calling SetImage method.

  • 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-04T23:07:35+00:00Added an answer on June 4, 2026 at 11:07 pm

    I was calling my view from other activity and that’s why I was getting NullPointerException. But now i’m calling it from same activity and its working fine.

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

Sidebar

Related Questions

In a Silverlight 4 project I have a class that extends Canvas: public class
In my app I have a UIView derived class Canvas that uses touchesBegan: withEvent:
My problem is as follows. I have a Canvas and I have a class
I am trying to use a Canvas to display objects that have world location
I have added multiple ovals on map view by using below code but I
Let's say I create a class that has its own canvas with: this.canvas =
I have use the TryUpdateModel method on the controllers but now I need to
Hello I have use DOMDocs in the past but I am stuck how to
I have troubles understanding gi.repository I use this contruction in my code from gi.repository
I have created one view, view code is, public class MyDraw extends View{ //List<Point>

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.