Home | History | Annotate | Download | only in tutorials
      1 page.title=Hello, World
      2 @jd:body
      3 
      4 <div id="qv-wrapper">
      5   <div id="qv">
      6     <h2></h2>
      7     <ol>
      8       <li><a href="#avd">AVD </a></li>
      9       <li><a href="#create"></a></li>
     10       <li><a href="#ui">UI </a></li>
     11       <li><a href="#run"></a></li>
     12       <li><a href="#upgrading">UI  XML </a></li>
     13       <li><a href="#debugging"></a></li>
     14       <li><a href="#noeclipse">Eclipse </a></li>
     15     </ol>
     16   </div>
     17 </div>
     18 
     19 <p>Hello, WorldAndroid  Eclipse </p>
     20 
     21 <p>Eclipse <a href="{@docRoot}guide/developing/other-ide.html">Developing in Other IDEs</a>Eclipse </p>
     22 
     23 <p> SDK Eclipse ADT <a href="{@docRoot}sdk/installing.html">Installing the Android SDK</a></p>
     24 
     25 <h2 id="avd">AVD </h2>
     26 
     27 <div class="sidebox-wrapper">
     28   <div class="sidebox">
     29     <p>AVD <a href="{@docRoot}guide/developing/tools/avd.html">Android </a> </p>
     30   </div>
     31 </div>
     32 
     33 <p> Android  Android AVDAVD  </p>
     34 
     35 <p>AVD Android SDK android SDK  <code>tools/</code> 
     36 <pre>
     37 android create avd --target 2 --name my_avd
     38 </pre>
     39 
     40 <p>   No AVD Android 1.5 my_avd AVD AVD </p>
     41 
     42 <p> <code>--target</code> <code>--name</code>  AVD </p>
     43 
     44 
     45 <h2 id="create"> Android </h2>
     46 
     47 <p>AVD  Eclipse  Android </p>
     48 
     49 <ol>
     50     <li>Eclipse <strong>[[]File] &gt; [[]New] &gt; [[]Project]</strong>  
     51       <p>ADT Plugin for Eclipse AndroidAndroid Android Project1  Android Android XML File</p>
     52     </li>
     53 
     54     <li>Android Android Project[[]<strong>Next</strong>] <br/><a href="images/hello_world_0.png"><img src="images/hello_world_0.png" style="height:230px" alt=""  /></a>
     55     </li>
     56 
     57     <li>
     58         <ul>
     59           <li><em>Project name:</em> HelloAndroid</li>
     60           <li><em>Application name:</em> Hello, Android</li>
     61           <li><em>Package name:</em> com.example.helloandroid </li>
     62           <li><em>Create Activity:</em> HelloAndroid</li>
     63           <li><em>SDK Min SDK Version:</em> 2</li>
     64         </ul>
     65         <p>[[]<strong>Finish</strong>] </p>
     66 
     67         <a href="images/hello_world_1.png"><img src="images/hello_world_1.png" style="height:230px" alt=""  /></a>
     68 
     69         <p></p>
     70       
     71         <dl>
     72             <dt><em>Project Name</em></dt>
     73                 <dd>Eclipse  </dd>
     74             <dt><em>Application Name</em></dt>
     75                 <dd> Android </dd>
     76             <dt><em>Package Name</em></dt>
     77                 <dd>  Java  Activity 
     78                   <p> Android  com.example </p></dd>
     79             <dt><em>Create Activity</em></dt>
     80                 <dd>   Android  {@link android.app.Activity}  UI </dd>
     81             <dt><em>SDK Min SDK Version</em></dt>
     82                 <dd> API  API  API  API 2 Android 1.1 Android   Android SDK API  API   API <em></em></dd>
     83         </dl>
     84       
     85         <p><em></em>:    [[]SDK Min SDK Version] </p>
     86 
     87         <p class="note">  Android 1.1  Android 1.1   AVD  Android 1.5 Android 1.1   1.5 </p>
     88     </li>
     89 </ol>
     90 
     91 <p> Android  Package Explorer<em>HelloAndroid &gt; src &gt; com.example.helloandroid</em>  <code>HelloAndroid.java</code> </p>
     92 
     93 <pre>
     94 package com.example.helloandroid;
     95 
     96 import android.app.Activity;
     97 import android.os.Bundle;
     98 
     99 public class HelloAndroid extends Activity {
    100     /** Called when the activity is first created. */
    101     &#64;Override
    102     public void onCreate(Bundle savedInstanceState) {
    103         super.onCreate(savedInstanceState);
    104         setContentView(R.layout.main);
    105     }
    106 }</pre>
    107 
    108 <p> {@link android.app.Activity} Activity 1  1 Android  {@link android.app.Activity#onCreate(Bundle) onCreate()}  UI   </p>
    109 
    110 <p> </p>
    111 
    112 
    113 <h2 id="ui">UI </h2>
    114 
    115 <p> HelloAndroid </p>
    116 
    117 <pre>
    118 package com.android.helloandroid;
    119 
    120 import android.app.Activity;
    121 import android.os.Bundle;
    122 <strong>import android.widget.TextView;</strong>
    123 
    124 public class HelloAndroid extends Activity {
    125    /** Called when the activity is first created. */
    126    &#64;Override
    127    public void onCreate(Bundle savedInstanceState) {
    128        super.onCreate(savedInstanceState);
    129        <strong>TextView tv = new TextView(this);
    130        tv.setText(&quot;Hello, Android&quot;);
    131        setContentView(tv);</strong>
    132    }
    133 }</pre>
    134 
    135 <p class="note"><strong>:</strong>  <strong>Ctrl+Shift+O</strong>Mac  <strong>+Shift+O</strong> Eclipse  </p>
    136 
    137 <p>Android  Views{@link android.view.View} UI   View  {@link android.widget.TextView} </p>
    138 
    139 <p>  TextView   Android {@link android.content.Context} Context Activity  Context  HelloAndroid Activity Context <code>this</code>  Context  TextView </p>
    140 
    141 <p>{@link android.widget.TextView setText(CharSequence) setText()}  </p>
    142 
    143 <p> Activity UI TextView  {@link android.app.Activity#setContentView(View) setContentView()} Activity UI </p>
    144 
    145 <p>Android Hello, World</p>
    146 
    147 
    148 <h2 id="run"></h2>
    149 
    150 <p>Eclipse </p>
    151 
    152 <ol>
    153   <li><strong>[[]] &gt; [[]]</strong> </li>
    154   <li>Android Application</li>
    155 </ol>
    156 
    157 <div class="sidebox-wrapper">
    158   <div class="sidebox">
    159     <p>Eclipse <a href="{@docRoot}guide/developing/eclipse-adt.html#RunConfig">ADT  Eclipse </a></p>
    160   </div>
    161 </div>
    162 
    163 <p>Eclipse ADT Android </p>
    164 
    165   <a href="images/hello_world_5.png"><img src="images/hello_world_5.png" style="height:230px" alt=""  /></a>
    166 
    167 <p>Hello, Android Eclipse  <code>res/values/strings.xml</code> <code>AndroidManifest.xml</code>  TextView </p>
    168 
    169 <p>Hello WorldAndroid </p>
    170 
    171 
    172 <h2 id="upgrading">UI  XML </h2>
    173 
    174 <p>Hello, World UI  UI  UI  </p>
    175 
    176 <p>Android XML   UI  XML  </p>
    177 
    178 <pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    179 &lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android";
    180   android:layout_width=&quot;fill_parent&quot;
    181   android:layout_height=&quot;fill_parent&quot;
    182   android:text=&quot;@string/hello&quot;/&gt;</pre>
    183 
    184 <p>Android XML  XML  View  View  1 XML {@link android.view.View}  View  UI  UI  Web </p>
    185 
    186 <p> XML <code>TextView</code>  View  1  4  XML  4 </p>
    187 
    188 <table>
    189     <tbody>
    190         <tr>
    191             <th>
    192                 
    193             </th>
    194             <th>
    195                 
    196             </th>
    197         </tr>
    198         <tr>
    199             <td>
    200                 <code>xmlns:android</code>
    201             </td>
    202             <td>
    203                 Android  Android  XML  Android  <br>
    204             </td>
    205         </tr>
    206         <tr>
    207             <td>
    208                 <code>android:layout_width</code>
    209             </td>
    210             <td>
    211                  View  View fill_parent<br>
    212             </td>
    213         </tr>
    214         <tr>
    215             <td>
    216                 <code>android:layout_height</code>
    217             </td>
    218             <td>
    219                 android:layout_width 
    220             </td>
    221         </tr>
    222         <tr>
    223             <td>
    224                 <code>android:text</code>
    225             </td>
    226             <td>
    227                 TextView <em>hello</em> <em>res/values/strings.xml</em>  <a href="{@docRoot}guide/topics/resources/resources-i18n.html"></a>
    228             </td>
    229         </tr>
    230     </tbody>
    231 </table>
    232 
    233 
    234 <p> XML   <code>res/layout/</code> resresources </p>
    235 <div class="sidebox-wrapper">
    236 <div class="sidebox">
    237   <h2></h2>
    238   <p> XML  /res/layout-land Android </p>
    239 </div>
    240 </div>
    241 
    242 <p>Eclipse   1 main.xmlHello World Android  XML  XML </p>
    243 
    244 <ol>
    245   <li>Eclipse  Package Explorer<code>/res/layout/</code> <code>main.xml</code>  XML main.xml XML 
    246 
    247 <pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    248 &lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android";
    249   android:layout_width=&quot;fill_parent&quot;
    250   android:layout_height=&quot;fill_parent&quot;
    251   android:text=&quot;@string/hello&quot;/&gt;</pre>
    252 <p></p>
    253 </li>
    254 
    255 <li><code>res/values/</code>  <code>strings.xml</code>  Eclipse ADT  <em>hello</em>  <em>app_name</em>  2 <em>hello</em> Hello, Android! I am a string resource!
    256 <pre>
    257 &lt;?xml version="1.0" encoding="utf-8"?>
    258 &lt;resources>
    259     &lt;string name="hello">Hello, Android! I am a string resource!&lt;/string>
    260     &lt;string name="app_name">Hello, Android&lt;/string>
    261 &lt;/resources>
    262 </pre>
    263 </li>
    264 
    265 <li><code>HelloAndroid</code> XML 
    266 <pre>
    267 package com.example.helloandroid;
    268 
    269 import android.app.Activity;
    270 import android.os.Bundle;
    271 
    272 public class HelloAndroid extends Activity {
    273     /** Called when the activity is first created. */
    274     &#64;Override
    275     public void onCreate(Bundle savedInstanceState) {
    276         super.onCreate(savedInstanceState);
    277         setContentView(R.layout.main);
    278     }
    279 }</pre>
    280 
    281 <p>R.layout.main</p>
    282 
    283 <p>View  <code>setContentView()</code>   <code>R.layout.main</code> <code>/res/layout/main.xml</code> Eclipse  R.java Eclipse Ant  R.java R </p>
    284 </li>
    285 </ol>
    286 
    287 <p> <strong>[[]Run] &gt; [[]Run History] &gt; [[]Android Activity]</strong> TextView 2 </p>
    288 
    289 <p class="note"><strong>:</strong>  <strong>Ctrl+F11</strong>Mac  <strong>+Shift+F11</strong></p>
    290 
    291 <p><a href="{@docRoot}guide/topics/fundamentals.html"></a>Android <a href="{@docRoot}guide/index.html"> </a><em> </em> </p>
    292 
    293 
    294 <div class="special">
    295 <h3>R </h3>
    296 <p>Eclipse <code>R.java</code> <code>gen/</code> Java Generated Java Files</p>
    297 
    298 <pre>
    299 package com.example.helloandroid;
    300 
    301 public final class R {
    302     public static final class attr {
    303     }
    304     public static final class drawable {
    305         public static final int icon=0x7f020000;
    306     }
    307     public static final class layout {
    308         public static final int main=0x7f030000;
    309     }
    310     public static final class string {
    311         public static final int app_name=0x7f040001;
    312         public static final int hello=0x7f040000;
    313     }
    314 }
    315 </pre>
    316 
    317 <p> <code>R.java</code>  Eclipse </p>
    318 
    319 <p> 16 layoutmainEclipse  main.xml  XML  <code>res/values/string.xml</code>  <code>res/drawable/</code> <code>R.java</code> </p>
    320 <p>Eclipse Ant  </p>
    321 <p><em></em></p>
    322 </div>
    323 
    324 <h2 id="debugging"></h2>
    325 
    326 <p>Android Plugin for Eclipse Eclipse  HelloAndroid  </p>
    327 
    328 <pre>
    329 package com.android.helloandroid;
    330 
    331 import android.app.Activity;
    332 import android.os.Bundle;
    333 
    334 public class HelloAndroid extends Activity {
    335     /** Called when the activity is first created. */
    336     &#64;Override
    337     public void onCreate(Bundle savedInstanceState) {
    338         super.onCreate(savedInstanceState);
    339         Object o = null;
    340         o.toString();
    341         setContentView(R.layout.main);
    342     }
    343 }</pre>
    344 
    345 <p> NullPointerException </p>
    346 
    347   <a href="images/hello_world_8.png"><img src="images/hello_world_8.png" style="height:230px" alt=""  /></a>
    348 
    349 <p> </p>
    350 
    351 <p>  <code>Object o = null;</code>    <strong>[[]Run] &gt; [[] Debug History] &gt; [[]Hello, Android]</strong>   Eclipse  Debug Perspective</p>
    352 
    353   <a href="images/hello_world_9.png"><img src="images/hello_world_9.png" style="height:230px" alt=""  /></a>
    354 
    355 
    356 <h2 id="noeclipse">Eclipse </h2>
    357   
    358   <p>Eclipse    Eclipse Eclipse </p>
    359   
    360   <p>Android Plugin for Eclipse  Android SDK  aaptadbddms <a href="{@docRoot}guide/developing/tools/index.html"></a>Ant </p>
    361   
    362   <p>Android SDK android   Ant  <code>build.xml</code>  </p>
    363   
    364   <p>Eclipse  HelloAndroid </p>
    365   
    366   <pre>
    367 android create project \
    368     --package com.android.helloandroid \
    369     --activity HelloAndroid \ 
    370     --target 2 \
    371     --path <em>&lt;path-to-your-project></em>/HelloAndroid 
    372 </pre>
    373 
    374   <p><em>path</em> </p>
    375   
    376   <p>SDK <a href="{@docRoot}guide/developing/other-ide.html">Developing in Other IDEs</a></p>
    377