EmberZ
EmberZ

Reputation: 109

NullPointer In View

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)

Upvotes: 0

Views: 302

Answers (3)

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);
    }

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

try changing

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

with

<com.DrawTastic.ClientActivity$MyView
   android:id="@+id/MyView1" 
   android:layout_gravity="center"       
   android:layout_height="match_parent" 
   android:layout_width="match_parent"/>

Upvotes: 0

pepyakin
pepyakin

Reputation: 2235

Try to copy your initialization logic

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

to other constructors such as

public MyView(Context context, AttributeSet attrs, int defStyle)
public MyView(Context context, AttributeSet attrs)

Upvotes: 0

Related Questions