Some problems with the Android Custom View construction method

Source: Internet
Author: User
<span id="Label3"></p><p><p>In Android development, you will often customize the view, then you will use construction methods, such as custom myview, inherit view, will require the implementation of the construction method:</p></p><pre><pre> <span style="color: #0000ff;"></span> public <span style="color: #000000;">MyView (context Context) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">(context);<br>LOG.V ("MyView", "MyView (context context)"); </span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">MyView (context context, attributeset attrs) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">(context, attrs);<br>LOG.V ("MyView", "MyView (context context, attributeset attrs)"); </span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">int</span> <span style="color: #000000;">Defstyleattr) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">(context, attrs, defstyleattr); }</span></pre></pre><p><p>so, in the process of real use, how exactly is it called?</p></p><p><p>Add directly to the OnCreate in mainactivity</p></p><p><p>MyView MyView = new MyView (this);</p></p><p><p>The log that was played:</p></p><p><p>06-14 13:19:53.867 21160-21160/? V/myview:myview (context Context)</p></p><p><p>This means that the first constructor method is called</p></p><p><p>When we add in the layout (XML)</p></p><pre><pre><span style="color: #0000ff;"><</span> <span style="color: #800000;">Li.deng.com.test.view.MyView </span> <span style="color: #ff0000;">Android:layout_width</span> <span style="color: #0000ff;">= "match_parent"</span> <span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">= "wrap_content"</span><span style="color: #0000ff;">/></span></pre></pre><p><p>Comment Out//myview MyView = new MyView (this);</p></p><p><p>Print Log:</p></p><p><p>06-14 13:25:07.196 22697-22697/? V/myview:myview (context context, AttributeSet attrs)</p></p><p><p>before, every time you look at someone Else's code is this:</p></p><pre><pre> <span style="color: #0000ff;">private </span> <span style="color: #000000;"> Context mcontext; </span> <span style="color: #0000ff;">public </span> <span style="color: #000000;"> MyView (context Context) {</span> <span style="color: #0000ff;">super </span> <span style="color: #000000;" (context); log.v (< span> "MyView", "MyView (context)" <span style="color: #000000;" "); < span> <span style="color: #0000ff;">this </span>. mcontext =<span style="color: #000000;"> context; </span> <span style="color: #0000ff;">public </span> <span style="color: #000000;"> MyView (context context, AttributeSet attrs) {</span> <span style="color: #0000ff; >super </span> <span style=" color: #000000; "> (context, att rs); LOG.V (</span> "MyView", "MyView (context context, attributeset attrs)" <span style="color: #000000;">); This.mcontext </span> =<span style="color: #000000;"> context; }</span></span></span></pre></pre><p><p>before, not particularly understand why to write so many construction methods, now understand the use of the construction method, this is because in the process of development, their own definition of</p></p><pre><pre><span>Public<span> MyView (context Context) { Super<span>(context); LOG.V ("MyView", "MyView (context context)"<span>); This.mcontext =<span> context; }<br></span></span></span></span></span>While using the process is defined in the layout file when the view, so that no context object, The result is a null pointer, and then looked at the construction method related knowledge, You must implement the second construction method</pre></pre><pre><pre><span>Public<span> MyView (context context, attributeset attrs) { Super<span>(context, attrs); LOG.V ("MyView", "MyView (context context, attributeset attrs)"<span>); This.mcontext =<span> context; }</span> </span> </span> </span></span><br><br>Reference Article: http://825288003.iteye.com/blog/1595213<br></pre></pre><pre><pre><br><br></pre></pre><p><p></p></p><p><p>Some problems with the Android Custom View construction method</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.