layout可以显示,程序调用就出错

hackerlyf 2014-09-26 04:09:28
我用网上的ColorPickerDialog控件改成了View控件,希望把ColorPicker控件直接嵌入到layout内,在layout设计界面可以看到View已经显示了,但在MainActivity内使用SetContentView调用后就报错,请大侠帮我查下哪里写得不对;


package com.example.colorpicker;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.view.View;


public class ColorPickerView extends View {
private Paint mPaint;//
private Paint mCenterPaint;//
private Paint mLinePaint;//
private Paint mRectPaint;//

private Shader rectShader;//
private float rectLeft;//
private float rectTop;//
private float rectRight;//
private float rectBottom;//
private final boolean debug = true;
private final String TAG = "ColorPicker";

Context context;
private String title;//
private int mInitialColor;//
// private OnColorChangedListener mListener;

private final int[] mCircleColors;//
private final int[] mRectColors;//

private int mHeight;//
private int mWidth;//
private float r;//
private float centerRadius;//

private boolean downInCircle = true;//
private boolean downInRect;//
private boolean highlightCenter;//
private boolean highlightCenterLittle;//

// public ColorPickerView(Context context, int height, int width) {
public ColorPickerView(Context context) {
super(context);
int height = 400;
int width = 500;
this.mHeight = height - 36;
this.mWidth = width;
setMinimumHeight(height - 36);
setMinimumWidth(width);

mCircleColors = new int[] {0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,
0xFF00FFFF, 0xFF00FF00,0xFFFFFF00, 0xFFFF0000};
Shader s = new SweepGradient(0, 0, mCircleColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(s);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(50);
r = width / 2 * 0.7f - mPaint.getStrokeWidth() * 0.5f;


mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setColor(mInitialColor);
mCenterPaint.setStrokeWidth(5);
centerRadius = (r - mPaint.getStrokeWidth() / 2 ) * 0.7f;


mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint.setColor(Color.parseColor("#72A1D1"));
mLinePaint.setStrokeWidth(4);


mRectColors = new int[]{0xFF000000, mCenterPaint.getColor(), 0xFFFFFFFF};
mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectPaint.setStrokeWidth(5);
rectLeft = -r - mPaint.getStrokeWidth() * 0.5f;
rectTop = r + mPaint.getStrokeWidth() * 0.5f +
mLinePaint.getStrokeMiter() * 0.5f + 15;
rectRight = r + mPaint.getStrokeWidth() * 0.5f;
rectBottom = rectTop + 50;
}

private void init()
{

}
@Override
protected void onDraw(Canvas canvas) {

canvas.translate(mWidth / 2, mHeight / 2 - 50);

canvas.drawCircle(0, 0, centerRadius, mCenterPaint);

if (highlightCenter || highlightCenterLittle) {
int c = mCenterPaint.getColor();
mCenterPaint.setStyle(Paint.Style.STROKE);
if(highlightCenter) {
mCenterPaint.setAlpha(0xFF);
}else if(highlightCenterLittle) {
mCenterPaint.setAlpha(0x90);
}
canvas.drawCircle(0, 0,
centerRadius + mCenterPaint.getStrokeWidth(), mCenterPaint);

mCenterPaint.setStyle(Paint.Style.FILL);
mCenterPaint.setColor(c);
}

canvas.drawOval(new RectF(-r, -r, r, r), mPaint);

if(downInCircle) {
mRectColors[1] = mCenterPaint.getColor();
}
rectShader = new LinearGradient(rectLeft, 0, rectRight, 0, mRectColors, null, Shader.TileMode.MIRROR);
mRectPaint.setShader(rectShader);
canvas.drawRect(rectLeft, rectTop, rectRight, rectBottom, mRectPaint);
float offset = mLinePaint.getStrokeWidth() / 2;
canvas.drawLine(rectLeft - offset, rectTop - offset * 2,
rectLeft - offset, rectBottom + offset * 2, mLinePaint);//?·|
canvas.drawLine(rectLeft - offset * 2, rectTop - offset,
rectRight + offset * 2, rectTop - offset, mLinePaint);//???
canvas.drawLine(rectRight + offset, rectTop - offset * 2,
rectRight + offset, rectBottom + offset * 2, mLinePaint);//??3
canvas.drawLine(rectLeft - offset * 2, rectBottom + offset,
rectRight + offset * 2, rectBottom + offset, mLinePaint);//???
super.onDraw(canvas);
}


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


private boolean inColorCircle(float x, float y, float outRadius, float inRadius) {
double outCircle = Math.PI * outRadius * outRadius;
double inCircle = Math.PI * inRadius * inRadius;
double fingerCircle = Math.PI * (x * x + y * y);
if(fingerCircle < outCircle && fingerCircle > inCircle) {
return true;
}else {
return false;
}
}


private boolean inCenter(float x, float y, float centerRadius) {
double centerCircle = Math.PI * centerRadius * centerRadius;
double fingerCircle = Math.PI * (x * x + y * y);
if(fingerCircle < centerCircle) {
return true;
}else {
return false;
}
}


private boolean inRect(float x, float y) {
if( x <= rectRight && x >=rectLeft && y <= rectBottom && y >=rectTop) {
return true;
} else {
return false;
}
}

private int interpCircleColor(int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}

float p = unit * (colors.length - 1);
int i = (int)p;
p -= i;

// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i+1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);

return Color.argb(a, r, g, b);
}

private int interpRectColor(int colors[], float x) {
int a, r, g, b, c0, c1;
float p;
if (x < 0) {
c0 = colors[0];
c1 = colors[1];
p = (x + rectRight) / rectRight;
} else {
c0 = colors[1];
c1 = colors[2];
p = x / rectRight;
}
a = ave(Color.alpha(c0), Color.alpha(c1), p);
r = ave(Color.red(c0), Color.red(c1), p);
g = ave(Color.green(c0), Color.green(c1), p);
b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}

private int ave(int s, int d, float p) {
return s + Math.round(p * (d - s));
}


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getmInitialColor() {
return mInitialColor;
}

public void setmInitialColor(int mInitialColor) {
this.mInitialColor = mInitialColor;
}
}
...全文
196 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hackerlyf 2014-09-27
  • 打赏
  • 举报
回复
已经解决了,非常感谢;
hackerlyf 2014-09-27
  • 打赏
  • 举报
回复
引用 11 楼 birdsaction 的回复:
public ColorPickerView(Context context, AttributeSet attributeSet) { super(context,attributeSet); ColorPickerView(context);//................................... } ColorPickerView(context) 你不是说你写了这个构造函数了么。
是写了,代码见顶楼; 在public ColorPickerView(Context context, AttributeSet attributeSet)内增加ColorPickerview(context);语句后,提示ColorPickerView方法没有定义,根据错误点击创建这个方法后,会自动创建以下方法: private void ColorPickerView(Context context) { // TODO Auto-generated method stub }
Birds2018 2014-09-27
  • 打赏
  • 举报
回复
public ColorPickerView(Context context, AttributeSet attributeSet) { super(context,attributeSet); ColorPickerView(context);//................................... } ColorPickerView(context) 你不是说你写了这个构造函数了么。
hackerlyf 2014-09-26
  • 打赏
  • 举报
回复
引用 9 楼 birdsaction 的回复:
public ColorPickerView(Context context, AttributeSet attributeSet) { this.ColorPickerView(context);//这样写 }
刚试了下,这么写会报错,this内找不到ColorPickerView的对象;
Birds2018 2014-09-26
  • 打赏
  • 举报
回复
public ColorPickerView(Context context, AttributeSet attributeSet) { this.ColorPickerView(context);//这样写 }
hackerlyf 2014-09-26
  • 打赏
  • 举报
回复
引用 7 楼 birdsaction 的回复:
异常很明显是没有构造函数。 public ColorPickerView(Context context, AttributeSet attributeSet) Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
确实是,需要怎么改?只是public ColorPickerView(Context context)改成public ColorPickerView(Context context, AttributeSet attributeSet)就可以了吗?
Birds2018 2014-09-26
  • 打赏
  • 举报
回复
异常很明显是没有构造函数。 public ColorPickerView(Context context, AttributeSet attributeSet) Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
hackerlyf 2014-09-26
  • 打赏
  • 举报
回复
以下是报错的日志,没看懂: 09-26 04:26:48.720: E/AndroidRuntime(1524): FATAL EXCEPTION: main 09-26 04:26:48.720: E/AndroidRuntime(1524): Process: com.example.colorpicker, PID: 1524 09-26 04:26:48.720: E/AndroidRuntime(1524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.colorpicker/com.example.colorpicker.ColorPickerActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.colorpicker.ColorPickerView 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread.access$700(ActivityThread.java:135) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.os.Handler.dispatchMessage(Handler.java:102) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.os.Looper.loop(Looper.java:137) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread.main(ActivityThread.java:4998) 09-26 04:26:48.720: E/AndroidRuntime(1524): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 04:26:48.720: E/AndroidRuntime(1524): at java.lang.reflect.Method.invoke(Method.java:515) 09-26 04:26:48.720: E/AndroidRuntime(1524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 09-26 04:26:48.720: E/AndroidRuntime(1524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 09-26 04:26:48.720: E/AndroidRuntime(1524): at dalvik.system.NativeStart.main(Native Method) 09-26 04:26:48.720: E/AndroidRuntime(1524): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.colorpicker.ColorPickerView 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.createView(LayoutInflater.java:603) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 09-26 04:26:48.720: E/AndroidRuntime(1524): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.Activity.setContentView(Activity.java:1928) 09-26 04:26:48.720: E/AndroidRuntime(1524): at com.example.colorpicker.ColorPickerActivity.onCreate(ColorPickerActivity.java:51) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.Activity.performCreate(Activity.java:5243) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 09-26 04:26:48.720: E/AndroidRuntime(1524): ... 11 more 09-26 04:26:48.720: E/AndroidRuntime(1524): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 09-26 04:26:48.720: E/AndroidRuntime(1524): at java.lang.Class.getConstructorOrMethod(Class.java:472) 09-26 04:26:48.720: E/AndroidRuntime(1524): at java.lang.Class.getConstructor(Class.java:446) 09-26 04:26:48.720: E/AndroidRuntime(1524): at android.view.LayoutInflater.createView(LayoutInflater.java:568) 09-26 04:26:48.720: E/AndroidRuntime(1524): ... 22 more
hackerlyf 2014-09-26
  • 打赏
  • 举报
回复
引用 4 楼 birdsaction 的回复:
我记得如果你把这个view放到布局里面 ,你需要有这个构造函数。 public ColorPickerView(Context context, AttributeSet attributeSet)
我用public ColorPickerView(Context context)替代了;
Birds2018 2014-09-26
  • 打赏
  • 举报
回复
我记得如果你把这个view放到布局里面 ,你需要有这个构造函数。 public ColorPickerView(Context context, AttributeSet attributeSet)
hackerlyf 2014-09-26
  • 打赏
  • 举报
回复
以下是Layout的代码;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical" >

    <com.example.colorpicker.ColorPickerView
        android:id="@+id/colorpicker4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>
Jing丶無雙 2014-09-26
  • 打赏
  • 举报
回复
你能把相应layout的XML文件贴出来么
taugnan 2014-09-26
  • 打赏
  • 举报
回复
看看错误log是什么,就知道哪里错了。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧