gpt4 book ai didi

java - 尝试绘制自定义 View 时崩溃 - InflateException

转载 作者:行者123 更新时间:2023-12-02 10:07:11 25 4
gpt4 key购买 nike

我尝试过寻找这个问题。我在其他问题中看到过类似的经历,但他们使用不同的资源。我试图在屏幕上绘制一个矩形,通过单击不同的按钮来执行不同的操作。我对 Android 还很陌生,所以任何帮助将不胜感激。我的预览看起来不错,与我想要做的一模一样,但在模拟器中崩溃了。

我正在尝试在我的 Activity 中绘制一个矩形。我创建了一个应该绘制三角形的自定义 View 类。然后,我尝试将该 View 添加到我的 content_main.xml 中,但它引发了 inflateException 错误。

ColorRectView 类:

public class ColorRectView extends View {

private Rect rectangle;
private Paint paint;

public ColorRectView(Context context) {
super(context);
int x = 50;
int y = 50;
int sideLength = 200;

// Create a rectangle to hold the random color
rectangle = new Rect(x, y, sideLength, sideLength);

// Create the Paint and set it's color
paint = new Paint();
paint.setColor(Color.GRAY);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(rectangle, paint);
}

content_main.xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<colorRectPractice.ColorRectView
android:id="@+id/colorRectangle"
android:layout_width="match_parent"
android:layout_height="match_parent" />

// This was the original view container that I added using the design tab, gave me the same error
<!--<view-->
<!--class="colorRectPractice.ColorRectView"-->
<!--id="@+id/view4"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--tools:layout_editor_absoluteX="74dp"-->
<!--tools:layout_editor_absoluteY="178dp" />-->


</android.support.constraint.ConstraintLayout>

我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{colorRectPractice/colorRectPractice.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #17: Error inflating class colorRectPractice.ColorRectView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4754)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #17: Error inflating class xyz.softdev.colorswipe.ColorRectView
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class xyz.softdev.colorswipe.ColorRectView
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)
at android.view.LayoutInflater.createView(LayoutInflater.java:615)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at xyz.softdev.colorswipe.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4754)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

您的自定义 View 构造函数中缺少AttributeSet

这是制作简单饼图的示例。

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;

import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;

import com.talview.recruit.R;

public class DonutChart extends View {


private float radius;

Paint paint;
Paint shadowPaint;

Path myPath;
Path shadowPath;

RectF outterCircle;
RectF innerCircle;
RectF shadowRectF;

public DonutChart(Context context, AttributeSet attrs) {
super(context, attrs);

TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.DonutChart,
0, 0
);

try {
radius = a.getDimension(R.styleable.DonutChart_radius, 20.0f);
} finally {
a.recycle();
}

paint = new Paint();
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
paint.setStrokeWidth(radius / 14.0f);

shadowPaint = new Paint();
shadowPaint.setColor(0xf0000000);
shadowPaint.setStyle(Paint.Style.STROKE);
shadowPaint.setAntiAlias(true);
shadowPaint.setStrokeWidth(4.0f);
shadowPaint.setMaskFilter(new BlurMaskFilter(4, BlurMaskFilter.Blur.SOLID));


myPath = new Path();
shadowPath = new Path();


outterCircle = new RectF();
innerCircle = new RectF();
shadowRectF = new RectF();

float adjust = (.019f*radius);
shadowRectF.set(adjust, adjust, radius*2-adjust, radius*2-adjust);

adjust = .038f * radius;
outterCircle.set(adjust, adjust, radius*2-adjust, radius*2-adjust);

adjust = .276f * radius;
innerCircle.set(adjust, adjust, radius*2-adjust, radius*2-adjust);

}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);



// green
setGradient(0xff84BC3D,0xff5B8829);
drawDonut(canvas,paint, 0,60);

//red
setGradient(0xffe04a2f,0xffB7161B);
drawDonut(canvas,paint, 60,60);

// blue
setGradient(0xff4AB6C1,0xff2182AD);
drawDonut(canvas,paint, 120,60);

// yellow
setGradient(0xffFFFF00,0xfffed325);
drawDonut(canvas,paint, 180,180);



}

public void drawDonut(Canvas canvas, Paint paint, float start,float sweep){

myPath.reset();
myPath.arcTo(outterCircle, start, sweep, false);
myPath.arcTo(innerCircle, start+sweep, -sweep, false);
myPath.close();
canvas.drawPath(myPath, paint);
}

public void setGradient(int sColor, int eColor){
paint.setShader(new RadialGradient(radius, radius, radius-5,
new int[]{sColor,eColor},
new float[]{.6f,.95f},TileMode.CLAMP) );
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int desiredWidth = (int) radius*2;
int desiredHeight = (int) radius*2;

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int width;
int height;

//70dp exact
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
}else if (widthMode == MeasureSpec.AT_MOST) {
//wrap content
width = Math.min(desiredWidth, widthSize);
} else {
width = desiredWidth;
}

//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
height = Math.min(desiredHeight, heightSize);
} else {
height = desiredHeight;
}

//MUST CALL THIS
setMeasuredDimension(width, height);
}


}

关于java - 尝试绘制自定义 View 时崩溃 - InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55269293/

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