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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:58:53+00:00 2026-05-31T20:58:53+00:00

I have implemented a custom view within another activity into a XML layout. The

  • 0

I have implemented a custom view within another activity into a XML layout. The view works and displays the canvas color but as soon as the view is passed the information to draw to the canvas the program dies. The problem seems to be coming from mPath, and I think it might have something to do with the bitmap size being different from the xml layout.

The program seems to hand at anything to do with mPath.

This is the code for the activity with the custom view inside, I had to make some variables static in order for it to display, this could be a factor in why its not working.

public class ClientActivity extends GraphicsActivity
implements ColorPickerDialog.OnColorChangedListener {


@Override
public void setContentView(View view) {
    super.setContentView(view);
}

static ClientNetwork net = new ClientNetwork();
static Thread fred = new Thread(net);
static int col;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);



    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
            0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

}

private static Paint mPaint;
private MaskFilter  mEmboss;
private MaskFilter  mBlur;



public static class MyView extends View {



    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    private boolean con;

    public MyView(Context c) {
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    public MyView(Context context, AttributeSet attrs ) {
        super(context, attrs);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);

        con = fred.isAlive();

        if(con == false)
        {
            fred.start();
        }


        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        Drawring fromNet;
        if((fromNet = net.GetServerDraw())!= null){

            //Drawring fromNet = net.GetServerDraw();
            net.resetpos();
            int position = fromNet.getPos();
            //col = fromNet.getColor();
            //colorChanged(col);

            float x = fromNet.getMx();
            float y = fromNet.getMy();





            switch (position) {
            case 1: 
                try {
                    touch_start(x, y);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }


                break;
            case 2:
                try {
                    touch_move(x, y);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }


                break;
            case 3:
                try {
                    touch_up();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
            mCanvas.drawPath(mPath, mPaint);
            invalidate();
        }

        invalidate();
    }
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) throws IOException {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;

        Log.d(null, "1st Position");
    }
    private void touch_move(float x, float y) throws IOException {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;

            Log.d(null, "2nd Position");
        }
    }


    private void touch_up() throws IOException  {
        mPath.lineTo(mX, mY);

        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
        Log.d(null, "3rd Position");
    }

    public void colorChanged(int color) {
        mPaint.setColor(col);
    }
}

@Override
public void colorChanged(int color) {
    mPaint.setColor(col);

}
}

This is the code for the XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:weightSum="1">


<RelativeLayout android:id="@+id/relativeLayout2"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:layout_weight="0.99">

    <view class="com.DrawTastic.ClientActivity$MyView"

        android:id="@+id/MyView1" android:layout_gravity="center"   android:layout_height="match_parent" android:layout_width="match_parent"/>


</RelativeLayout>


<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="match_parent" android:layout_height="144dp">
    <EditText android:layout_height="wrap_content" android:id="@+id/input"
        android:layout_width="250dp" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true">
        <requestFocus></requestFocus>
    </EditText>
    <Button android:layout_height="wrap_content" android:id="@+id/send"
        android:layout_width="wrap_content" android:text="Send"
        android:onClick="sent" android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/input"
        android:layout_alignParentRight="true"></Button>
    <ListView android:id="@+id/listView" android:layout_width="match_parent"
        android:layout_above="@+id/input" android:layout_alignParentRight="true" android:layout_height="100dp"></ListView>
</RelativeLayout>

Any help is extremely appreciated!

Thanks

03-26 12:57:30.449: ERROR/AndroidRuntime(1880): FATAL EXCEPTION: main
03-26 12:57:30.449: ERROR/AndroidRuntime(1880): java.lang.NullPointerException
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.graphics.Canvas.drawPath(Canvas.java:950)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at com.DrawTastic.ClientActivity$MyView.onDraw(ClientActivity.java:168)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.View.draw(View.java:6880)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.View.draw(View.java:6883)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
 03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at  android.os.Handler.dispatchMessage(Handler.java:99)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.os.Looper.loop(Looper.java:123)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at java.lang.reflect.Method.invoke(Method.java:507)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-26 12:57:30.449: ERROR/AndroidRuntime(1880):     at dalvik.system.NativeStart.main(Native 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-05-31T20:58:55+00:00Added an answer on May 31, 2026 at 8:58 pm

    do initialization in other constructor too,

    public MyView(Context c) {
            super(c);
    
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
    
    
        public MyView(Context context, AttributeSet attrs ) {
            super(context, attrs);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a custom Layout that extends RelativeLayout. It displays lots of different
I have implemented a custom list view which looks like the twitter timeline. adapter
I have a custom view called DrawView created in the main activity. I have
I have multiple custom UIWebView's as subviews within a UIScrollView. I have implemented the
I have implemented Google Map view in my app with a number of custom
I have implemented a custom ActionMapper which obtains the locale from the URI (the
I have a custom component where I have implemented INotifyPropertyChanged and IBindableComponent . However,
I have a UIViewController whose view has a custom subview. This custom subview needs
I have a listview with custom array adapter, the view allows the user to
I have a LinearLayout with a custom view on top of it. The veiw

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.