NullPointer在视图中

时间:2012-03-26 11:54:00

标签: java android android-layout android-view

我已将另一个活动中的自定义视图实现为XML布局。视图工作并显示画布颜色,但是一旦视图传递了绘制到画布的信息,程序就会死亡。问题似乎来自mPath,我认为它可能与位图大小不同于xml布局有关。

程序似乎与mPath有关。

这是包含自定义视图的活动的代码,我必须将一些变量设置为静态以便显示,这可能是导致其无法工作的一个因素。

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

}
}

这是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>

非常感谢任何帮助!

由于

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)

3 个答案:

答案 0 :(得分:1)

在其他构造函数中进行初始化,

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

答案 1 :(得分:0)

尝试复制初始化逻辑

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

到其他构造函数,例如

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

答案 2 :(得分:0)

尝试改变      

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

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