gpt4 book ai didi

java - View 中的空指针

转载 作者:行者123 更新时间:2023-12-01 15:28:32 25 4
gpt4 key购买 nike

我已将另一个 Activity 中的自定义 View 实现为 XML 布局。 View 可以工作并显示 Canvas 颜色,但是一旦 View 传递了要绘制到 Canvas 的信息,程序就会终止。问题似乎来自 mPath,我认为这可能与位图大小与 xml 布局不同有关。

该程序似乎可以处理任何与 mPath 有关的事情。

这是内部带有自定义 View 的 Activity 的代码,我必须将一些变量设置为静态才能使其显示,这可能是其不起作用的一个因素。

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)

最佳答案

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

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

关于java - View 中的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com