我试图调用我在我的活动中创建的一个类,不幸的是,只要点击一个应该发送给我的活动的按钮,该应用程序就会停止运行。我试图调试它,它总是停在setContentView(R.layout.activity_draw_triangle)行。我已经在线查看并尝试使用不同的答案来解决我的问题,但无所事事。
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.PathMeasure;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import scribens.zarha.scribensapp.DrawingView;
import java.util.ArrayList;
public class DrawTriangleActivity extends AppCompatActivity {
DrawingView dv;
TextView instructions;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_triangle);
Button button = (Button) findViewById(R.id.button);
instructions = (TextView) findViewById(R.id.textView2);
dv = (DrawingView) findViewById(R.id.dv);
dv = new DrawingView(this);
//setContentView(dv);
}
public void GoBack(View view) {
// Brings the user to the activity where the draw a line
Intent intent = new Intent(this, MainActivity.class);
Button button = (Button) findViewById(R.id.button);
startActivity(intent);
}
}
以下是我创建的类的代码
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.util.AttributeSet;
public class DrawingView extends View {
private Path mPath;
private Paint mPaint;
private Bitmap mBitmap;
private Canvas mCanvas;
public int width;
public int height;
private Paint mBitmapPaint;
Context context;
private Paint circlePaint;
private Path circlePath;
public DrawingView(Context c) {
super(c);
context=c;
setupDrawing();
}
private void setupDrawing(){
//get drawing area setup for interaction
mPath = new Path();
mPaint = new Paint();
circlePaint = new Paint();
circlePath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
}
@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) {
//draw view
canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath( mPath, mPaint);
canvas.drawPath(circlePath, circlePaint);
}
public float mX, mY;
public static final float TOUCH_TOLERANCE = 4;
public void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
public 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;
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
}
}
public void touch_up() {
mPath.lineTo(mX, mY);
circlePath.reset();
// 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;
}
}
最后,我的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:screenOrientation="landscape"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mygod.myname.mygodapp.DrawActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suivez le tracé du triangle"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
<mygod.myname.mygodapp.DrawingView
android:layout_width="wrap_content"
android:layout_height="222dp"
android:id="@+id/dv" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retour"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="GoBack"
android:layout_gravity="right" />
</LinearLayout>
堆栈代码
07-18 15:55:56.003 28620-28620/mygod.myname.mygodapp W/ResourceType: Failure getting entry for 0x01080af7 (t=7 e=2807) (error -75)
07-18 15:55:56.083 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* installDecor mIsFloating : false
07-18 15:55:56.083 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* installDecor flags : -2139029248
07-18 15:55:56.133 28620-28620/mygod.myname.mygodapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-18 15:55:56.333 28620-28746/mygod.myname.mygodapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-18 15:55:56.353 28620-28620/mygod.myname.mygodapp V/ActivityThread: updateVisibility : ActivityRecord{1fd2b97 token=android.os.BinderProxy@3dde6f31 {mygod.myname.mygodapp/mygod.myname.mygodapp.MainActivity}} show : true
07-18 15:55:56.353 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
07-18 15:55:56.353 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
07-18 15:55:56.383 28620-28620/mygod.myname.mygodapp D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
07-18 15:55:56.383 28620-28746/mygod.myname.mygodapp I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 06/05/15 Fri
Local Branch: LA.BF.1.1.1_RB1.05.01.00.042.032
Remote Branch:
Local Patches:
Reconstruct Branch:
07-18 15:55:56.383 28620-28746/mygod.myname.mygodapp I/OpenGLRenderer: Initialized EGL, version 1.4
07-18 15:55:56.403 28620-28746/mygod.myname.mygodapp D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
07-18 15:55:56.403 28620-28746/mygod.myname.mygodapp D/OpenGLRenderer: Enabling debug mode 0
07-18 15:55:56.553 28620-28620/mygod.myname.mygodapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3dde6f31 time:188182652
07-18 15:55:56.553 28620-28620/mygod.myname.mygodapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3dde6f31 time:188182652
07-18 15:55:59.833 28620-28620/mygod.myname.mygodapp D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
07-18 15:55:59.893 28620-28620/mygod.myname.mygodapp I/Timeline: Timeline: Activity_launch_request id:mygod.myname.mygodapp time:188185997
07-18 15:55:59.943 28620-28620/mygod.myname.mygodapp W/ResourceType: Failure getting entry for 0x01080af7 (t=7 e=2807) (error -75)
07-18 15:55:59.953 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* installDecor mIsFloating : false
07-18 15:55:59.953 28620-28620/mygod.myname.mygodapp D/PhoneWindow: *FMB* installDecor flags : -2139029248
07-18 15:55:59.963 28620-28620/mygod.myname.mygodapp D/AndroidRuntime: Shutting down VM
07-18 15:55:59.963 28620-28620/mygod.myname.mygodapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: mygod.myname.mygodapp, PID: 28620
java.lang.RuntimeException: Unable to start activity ComponentInfo{mygod.myname.mygodapp/mygod.myname.mygodapp.DrawTriangleActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class mygod.myname.mygodapp.DrawingView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class mygod.myname.mygodapp.DrawingView
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at mygod.myname.mygodapp.DrawTriangleActivity.onCreate(DrawTriangleActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6550)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at mygod.myname.mygodapp.DrawTriangleActivity.onCreate(DrawTriangleActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6550)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
答案 0 :(得分:0)
在onCreate()中使用button.onClickLIstner()并在onclick listner中添加意图代码。
答案 1 :(得分:0)
您的DrawingView需要构造函数DrawingView(Context,AttributeSet)。这是从xml充气时调用的(各种参数是属性集)。你也应该有一个DrawingView(Context,AttributeSet,int),以防它们出现。