У меня есть программа рисования Finger, которую я скопировал из демоверсии API androids, я немного ее изменил и хочу вызывать из xml-файла, который вызывает представление вместо вызова представления прямо в основную Java. Я создал новый класс для MyView.java вместо того, чтобы иметь его непосредственно внутри основного файла Java, чтобы я мог вызвать его в XML. Теперь MyView.java установлен на extends View
, и у меня есть xml, который содержит представление, установленное на my.project.MyView
. и мой setContentView
внутри моей основной Java, установлен на setContentView(R.layout.main)
. Я получаю ФК, когда пытаюсь запустить программу. Разве я не могу это сделать? Должен ли 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);
}
}
Log Cat:
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);
EDIT
ставлю
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)
Это мой touch_up
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();
}
Строка 75 касания:
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
my 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();
Также программа запускается сейчас, но как только я касаюсь экрана, я получаю ФК.
-Спасибо