Android FAQ 2:resources$notfoundexception (1)

Source: Internet
Author: User
<span id="Label3"></p><p><p>This morning to do a homework, accidentally appeared a bug, although the change passed quickly, but at that time because busy to catch up with homework, also did not think carefully about the cause of the bug, to the evening finally have time to do a small demo Test.</p></p><p><p>Demo is very simple, the need Is: Mainactivity interface has only one button, when the button is clicked, a dialog box appears, the dialog box has an input box, the input box by default is Text.</p></p><p><p>:</p></p><p><p></p></p><p><p>The source code is as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.my.day24_my_testdialog;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View.OnClickListener;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.Button;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.EditText;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.AlertDialog;</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button bt;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Alertdialog dialog;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Setcontentview (r.layout.activity_main);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Initialize dialog box</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">InitDialog ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span>BT =<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.bt);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>Bt.setonclicklistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Onclicklistener () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">dialog.show ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> });</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">InitDialog () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>EditText input =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>EditText (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Input.settext ("this is the default text"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span>Input.setselectallonfocus (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span>Dialog =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Alertdialog.builder (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">. SetIcon (android. R.drawable.ic_dialog_info)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>. Settitle ("information Edit box"<span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">. Setview (input)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>. setcancelable (<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>. Setpositivebutton ("ok",<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>. Setnegativebutton ("cancel",<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">. Create ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>  </p></p><p><p>When modifying the 34th line of Code: to change it to a code like Input.settext (23), compile no problem, run a direct crash, The view log appears the following prompt:</p></p><p><p></p></p><p><p>Extract Key Information:</p></p><p><p></p></p><p><p>There is a <strong>resource$notfoundexception</strong> problem.</p></p><p><p>The idea was to know that the SetText () method passed in a string, but for the sake of simplicity, the system would automatically help to convert it to a string if you passed in an integer, but the fact is that the system does not make any changes to the integer number, the solution is simple, Add an empty string directly to the back, i.e. SetText (23+ "") is Ok.</p></p><p><p>Here's the essence: SetText () has several overloaded methods</p></p><p><p></p></p><p><p>  <strong>You can see that one of the methods is SetText (int resid), where the argument is exactly an integer number, when I pass this method to an integer, the method is called by default, and the ID should be a resource id, and the R resource file does not have this ID for a resource. So prompt resource$notfoundexception Information.</strong></p></p><p><p>Take a closer look at the first method, whose parameter type is Charsequence.</p></p><p><p>This type is common in Java methods as parameters, and we typically pass a string as a parameter of this type.</p></p><p><p>  </p></p><p><p>  <strong>What is a charsequence essence?</strong></p></p><p><p>Consult the JDK documentation, which is defined As:</p></p><p><p></p></p><p><p>Can know that it is an interface, its implementation class has charbuffer,segment,string,stringbuffer,stringbuilder, of which the following three classes we are familiar with, all about the character sequence.</p></p><p><p>  </p></p><p><p>The document describes this interface:</p></p><p><p></p></p><p><p>The method definition for the Charsequence interface:</p></p><p><p></p></p><p><p>  </p></p><p><p>The String class Defines:</p></p><p><p> </p></p><p><p>Stringbufferl class Definition:</p></p><p><p></p></p><p><p>StringBuilder class Definition:</p></p><p><p></p></p><p><p>  <strong>The String,stringbuffer,stringbuilder classes are defined for final, and they all implement the Charsequence Interface.</strong></p></p><p><p>In conclusion: the<strong>charsequence interface provides uniform read-only access to many different kinds of char sequences, that is, the type of charsequence as a parameter, and the ability to handle more scenarios, which not only can receive string parameters, You can also receive parameters of type StringBuilder and stringbuffer, which can be thought of as a simple interface callback (a reference to an object that implements an interface is paid to this interface variable). </strong></p></p><p><p>Extension: There is also a common class similar to charsequence is called <strong>Charset</strong>, which represents the type of encoding, is a string, can take the value of "utf-8", "gb2312" and so On.</p></p><p><p>     the <strong>Charset</strong> class is often seen in the related methods of Strings:</p></p><p><p>1. Constructs a new one by using the specified charset to decode the specified byte array <code>String</code> .</p></p><p><p>2. <code>String</code> encode this into a byte sequence with the given charset and store the result in a new byte array.</p></p><p><p>Method one: often used to get a string of byte array, the original string contains chinese, if you want to restore a byte array to a string, you must specify the character set is "utf-8", otherwise the byte array into a string will be Garbled.</p></p><p><p>Method Two: converts a string into a byte array in the specified encoding Mode.</p></p><p><p>Supplement: Common coding</p></p><p><p>1<strong> ASCII code</strong> :<br>US standard Information interchange code, denoted by a byte of 7 bits<br>2<strong> iso8859-1</strong>:<br>Latin code table, denoted by a byte of 8 bits<br>3 <strong>GB2312</strong>:<br>Chinese code<br>4 <strong>GBK</strong>:<br>The Chinese encoding table is upgraded to incorporate more Characters.<br>5 <strong>Unicode</strong>:<br>International standard code, All text is represented by two bytes<br>6 <strong>UTF-8</strong>:<br>A maximum of three bytes for one character</p></p><p><p></p></p><p><p> <strong>Not finished, to be continued.</strong></p></p><p><p></p></p><p><p>Android FAQ 2:resources$notfoundexception (1)</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.