gpt4 book ai didi

android - 从布局看?

转载 作者:行者123 更新时间:2023-11-30 04:41:32 24 4
gpt4 key购买 nike

我有一个从 androids API 演示中复制的手指绘画程序,我对其进行了一些更改,并希望从调用 View 的 xml 文件中调用,而不是直接将 View 调用到主 Java 中。我为 MyView.java 创建了一个新类,而不是直接将它放在主 java 文件中,这样我就可以将它调用到 xml 中。现在 MyView.java 设置为 extends View 并且我将保存 View 的 xml 设置为 my.project.MyView。我的主 java 中的 setContentView 设置为 setContentView(R.layout.main)。当我尝试运行该程序时,我得到了一个 FC。我做不到吗? MyView 是否应该设置为 extends SurfaceView主要 XML:

<?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"
>
<my.project.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

MyView.java:

public class MyView extends View {

private static final String TAG = "MyApp";

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

private Paint mPaint;

public MyView(Context c, AttributeSet attr) {
super(c, attr);
//**size of drawing area.**\\
mBitmap = Bitmap.createBitmap(480,650, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawColor(0xFFAAAAAA);

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

try{
canvas.drawPath(mPath, mPaint);
}catch(Exception exception){
Log.d(TAG, "mPath=" + mPath + ", mPaint=" + mPaint);
}

}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
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;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

MyMain java:

public class MyMain extends GraphicsActivity
implements ColorPickerDialog.OnColorChangedListener {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

日志猫:

05-05 09:26:30.668: ERROR/AndroidRuntime(8104): FATAL EXCEPTION: main
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): java.lang.NullPointerException
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.graphics.Canvas.drawPath(Canvas.java:950)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at www.hotapp.com.thepatch.html.MyView.onDraw(MyView.java:42)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.View.draw(View.java:6740)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.View.draw(View.java:6743)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.View.draw(View.java:6743)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewRoot.draw(ViewRoot.java:1407)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.os.Looper.loop(Looper.java:123)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-05 09:26:30.668: ERROR/AndroidRuntime(8104): at dalvik.system.NativeStart.main(Native Method)

第42行

canvas.drawPath(mPath, mPaint);

编辑

我把

try{
canvas.drawPath(mPath, mPaint);
}catch(Exception exception){
Log.d(TAG, "mPath=" + mPath + ", mPaint=" + mPaint);
}

在我的日志猫中我想到了

 05-05 10:15:03.418: ERROR/AndroidRuntime(8596): FATAL EXCEPTION: main
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): java.lang.NullPointerException
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.graphics.Canvas.drawPath(Canvas.java:950)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at www.hotapp.com.thepatch.html.MyView.touch_up(MyView.java:75)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at www.hotapp.com.thepatch.html.MyView.onTouchEvent(MyView.java:95)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.View.dispatchTouchEvent(View.java:3766)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.os.Looper.loop(Looper.java:123)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-05 10:15:03.418: ERROR/AndroidRuntime(8596): at dalvik.system.NativeStart.main(Native Method)

这是我的修饰

private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

touch_up 的第 75 行:

 // commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);

我的onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

第 95 行 onTouchEvent

touch_up();

程序现在也启动了,但是只要我触摸屏幕,我就会得到一个 FC。-谢谢

最佳答案

只是一个初步的预感,但要开始自定义 View 构造函数,至少还必须具有 AttributeSet 字段。

public class MyView extends View {
..............
..............
public MyView(Context c, AttributeSet attr) {
super(c, attr);
..............
..............
}

关于android - 从布局看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5899955/

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