java - How to use addView to add CustomView? -


i use below code in main.java, work!

framelayout view = (framelayout) findviewbyid(r.id.frame); textview product = new textview(this); product.settext("product"); view.addview(product); 

but want add customview, not work

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); 

how can add customview addview? thanks

the 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> 

error log:

03-20 23:26:11.278: e/androidruntime(6706): java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=2, result=0, data=null} activity {com.smallmouth./com.smallmouth..photoedit}: java.lang.nullpointerexception 

you extending view class (or child of view class) right? if yes, need call super method , pass context parent in constructor you're not doing. current version this

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); } } 

btw need first constructor in case


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -