如何使用addView添加CustomView?
How to use addView to add CustomView?
我在 main.java 中使用了下面的代码,成功了!
FrameLayout view = (FrameLayout) findViewById(R.id.frame);
TextView product = new TextView(this);
product.setText("Product");
view.addView(product);
但是我想添加CustomView,不行
FrameLayout view = (FrameLayout) findViewById(R.id.frame);
CustomView v = new CustomView(this); //can't create new object
v.setImageResource(R.mipmap.scale);
view.addView(v);
如何通过addView添加CustomView?
谢谢
自定义视图Class
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
obtainStyledAttributes(attrs);
init();
}
attrs.xml
<resources>
<declare-styleable name="CustomView">
<attr name="src" format="reference" />
<attr name="editable" format="boolean"/>
<attr name="frameColor" format="color" />
<attr name="frameWidth" format="dimension" />
<attr name="framePadding" format="dimension" />
<attr name="degree" format="float" />
<attr name="scale" format="float" />
<attr name="controlDrawable" format="reference"/>
<attr name="controlLocation">
<enum name="left_top" value="0" />
<enum name="right_top" value="1" />
<enum name="right_bottom" value="2" />
<enum name="left_bottom" value="3" />
</attr>
</declare-styleable>
</resources>
错误日志:
03-20 23:26:11.278: E/AndroidRuntime(6706): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.smallmouth./com.smallmouth..PhotoEdit}: java.lang.NullPointerException
您正在扩展视图 class(或视图 class 的某个子视图),对吗?如果是,您需要调用 super 方法并将上下文传递给构造函数中的父级,而您没有这样做。当前版本是这样的
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
顺便说一句,在你的情况下你只需要第一个构造函数
我在 main.java 中使用了下面的代码,成功了!
FrameLayout view = (FrameLayout) findViewById(R.id.frame);
TextView product = new TextView(this);
product.setText("Product");
view.addView(product);
但是我想添加CustomView,不行
FrameLayout view = (FrameLayout) findViewById(R.id.frame);
CustomView v = new CustomView(this); //can't create new object
v.setImageResource(R.mipmap.scale);
view.addView(v);
如何通过addView添加CustomView? 谢谢
自定义视图Class
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
obtainStyledAttributes(attrs);
init();
}
attrs.xml
<resources>
<declare-styleable name="CustomView">
<attr name="src" format="reference" />
<attr name="editable" format="boolean"/>
<attr name="frameColor" format="color" />
<attr name="frameWidth" format="dimension" />
<attr name="framePadding" format="dimension" />
<attr name="degree" format="float" />
<attr name="scale" format="float" />
<attr name="controlDrawable" format="reference"/>
<attr name="controlLocation">
<enum name="left_top" value="0" />
<enum name="right_top" value="1" />
<enum name="right_bottom" value="2" />
<enum name="left_bottom" value="3" />
</attr>
</declare-styleable>
</resources>
错误日志:
03-20 23:26:11.278: E/AndroidRuntime(6706): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.smallmouth./com.smallmouth..PhotoEdit}: java.lang.NullPointerException
您正在扩展视图 class(或视图 class 的某个子视图),对吗?如果是,您需要调用 super 方法并将上下文传递给构造函数中的父级,而您没有这样做。当前版本是这样的
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
顺便说一句,在你的情况下你只需要第一个构造函数