ドキュメント > もしもしAndroid!

「ドキュメント/もしもしAndroid!」の編集履歴(バックアップ)一覧はこちら

ドキュメント/もしもしAndroid!」(2007/11/21 (水) 15:11:18) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

*Hello, Android! 第一印象は重要だ。それは、あなたが、このアンドロイドというフレームワークを手にして、"Hello, World!"を書いたときに受ける第一印象だ。そう、アンドロイドにおいて、それはとても簡単なのだ。下記を見て欲しい。 **プロジェクトを作成する。 **UIを構築する。 **コードを走らせる: Hello, Android 以下のセクションでそれをつまびらかに語っていこう。 **UIをXMLのレイアウトにアップグレードする。 **プロジェクトをデバッグする。 **Eclipseなしでプロジェクトを作成する。 さあ行こう。 *プロジェクトを作成する プロジェクトを作成することはできる限り簡単にしてある。Eclipseプラグインで、Androidの開発環境のスナップを作成することが可能だ。[[Eclipse 3.3以上(Europa)>http://eclipse.org/]]と、[[Eclipse用のAndroidプラグイン>http://www29.atwiki.jp/android/pages/14.html#id_0f2eb0b7]]は用意してあるかい?それらをインストールしてから次に進んで欲しい。 最初に、"Hello, World!"をビルドするための、簡単な要約を述べておこう。 +File->New->Project menuから、"Android Project"を新しく作成する。 +New Android Project ダイアログで、プロジェクトの詳細を埋める。 +なにかを表示するための自動生成されたテンプレートコードを編集する。 さあ、行こう!以下でそれぞれのステップの詳細を説明しよう。 **1.新しい"Android Project"を作成する。 Eclipseから、File->New->Projectと選択して欲しい。もし、EclipseのAndroidプラグインがきちんとインストールされているなら、表示されるダイアログの中に、"Android"と名前のついたフォルダがあり、その中には、"Android Project"があるはずだ。 #ref(hello_world_0.png); "Android Project"を選択し、"Next"を押そう。 **2.New Android Project ダイアログで、プロジェクトの詳細を埋める。 次の画面で、プロジェクトに関係する詳細を入力する。たとえば次の例のように: #ref(hello_world_1.png); それぞれの入力欄が意味するところは次のようになる。 |Project Name|プロジェクトを保存したいディレクトリもしくはフォルダの名前| |Package Name|これはパッケージの名前空間だ。ちょうどJavaのように。あなたのソースコードは全てここより下位におかれるようにする。ここには、自動生成されたスタブのパッケージ名がすでにセットされているはずだ。パッケージ名は、システムにインストールされるすべてのパッケージ間で、ユニークである必要がる。というわけで、あなたのアプリケーションに標準的なドメイン命名スタイルを使うことはとても重要だ。上述の例では、パッケージ名として、ドメイン"com.google.android"を使用している。あなたの所属する組織にみあった、唯一の名前を使用するといいだろう。| |Activity Name|ここでは、プラグインによって生成されるスタブクラスの名前が書かれている。これは、AndroidのActivityクラスのサブクラスである。Activityは単純なクラスで、それ自体で実行させ、処理させることができる。希望するならUIも作れるが、そうしなくても構わない| |Application Name|ここにはユーザーが目にするアプリケーションのタイトルを入力する。| "Use default location"チェックボックスをONにすることで、プロジェクトファイルの保存場所を変更することができる。 **3.自動生成コードを編集する。 After the plugin runs, you'll have a class named HelloAndroid that looks like this: プラグインを実行すると、下記のような、HelloAndroidクラスが出来上がっているがわかるだろう。 #highlight(java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } } }} 次のステップで、これを修正していこう! *UIを構築する。 プロジェクトをセットアップしたあとは、当然、それを修正していく。以下がその完成品だ。1行ずつ解剖していこう。 #highlight(java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } } }} このサンプルのコンパイルをするために、インポートセクションに、"import android.widget.TextView;"を追加するしなければらいことを注意しよう。 Androidでは、ユーザーインターフェースは、Viewsと呼ばれるクラス階層で成り立っている。Viewはシンプルな描画オブジェクトである。たとえば、ラジオボタンであるとか、アニメーションであるとか、(今回のケースは)テキストラベルなどだ。テキストを扱えるVieewのサブクラスの名前は、単に、TextViewとなる。 下記が、TextViewクラスを生成する方法だ。 #highlight(java){ TextView tv = new TextView(this); } TextViewクラスのコンストラクタに渡している引数は、Android Contextのインスタンスである。Contextは単にシステムへ渡すハンドルである。そのハンドルは、リソースを解決したり、データベースや設定などにアクセスするために供給されている。ActivityクラスはContextから派生している。それゆえ、HelloAndroidクラスはActivityクラスのサブクラスであり、コンテキストであるのだ。だから、"this"参照をTextViewに渡すことができる。 一度TextViewを生成してしまえば、何を表示するのか伝えてあげる必要がある。 #highlight(java){ tv.setText("Hello, Android"); } とくに特筆すべきことはないだろう。 ここまでで、TextViewを生成し、どんなテキストをディスプレイに表示すべきかを伝えた。最後のステップは、実際のディスプレイに、TextViewをつなぐことである。こんな感じに。 #highlight(java){ setContentView(tv); } ActivityのsetContentViewメソッドは、ActivityのUIにどのViewが関連付けられるべきかをシステムに通知する。もし、Activityがこのメソッドをコールしないなら、UIは何も表示されないし、システムは真っ白けの画面を表示することだろう。今のところの目的は、何でもいいからテキストを表示することなので、作ったばかりのTextViewを渡してしまえばよい。 これで、アンドロイドでの"Hello, World"のコーディングは完了だ。もちろん、つぎは、実行させるてみよう。 *コードの実行:Hello, Android Eclipseプラグインのおかげで、とても簡単にあなたのアプリケーションを実行することができる。メニューからRunを選択すると、下のようなダイアログが表示される。 #ref(hello_world_2.png); 次に、"Android Application"を選択しよう。そして、アイコンの左上をクリックしよう(+印とともに、画面に描画されているやつだ)。それとも、単に、"Android Application"をダブルクリックするだけでいい。"New_configuration"と名づけられた新しいランチャーダイアログが表示されるはずだ。 #ref(hello_world_3.png); 名前を何か適当なもの、たとえば、"Hello, Android"と変更して、"Browse"ボタンを押下して、あなたのプロジェクトを選択しよう。(もしあなたが2個以上のAndroidプロジェクトをEclipseで開いていたら、正しいものを選択しているかどうか確かめてほしい)プラグインは、自動的に、あなたのプロジェクトからActivityのサブクラスをスキャンして、"Activity:"ラベルの下のドロップダウンリストに追加してくれる。デフォルトでは、あなたは、"Hello, Android"プロジェクトしか作っていないから、単に続けるだけでいい。 "Apply"ボタンを押下しよう。こういう風になる。 #ref(hello_world_4.png); これで成功だ。"Run"ボタンを押してみよう。Androidエミュレータがスタートするはずだ。起動完了したら、あなたのアプリケーションが表示されるだろう。今までいったことが全部できていれば、次のような画面を目にすることができるはずだ。 #ref(hello_world_5.png); これが、Androidにおける、"Hello, World"だ。とっても簡単だったろう?チュートリアルの次のセクションでは、Androidについて、より詳細な価値ある情報を知ることができるだろう。 //*Upgrading the UI to an XML Layout *UIをXMLレイアウトにアップグレードする。 //The "Hello, World" example you just completed uses what we call "programmatic" UI layout. This means that you construct and build your application's UI directly in source code. If you've done much UI programming, you're probably familiar with how brittle that approach can sometimes be: small changes in layout can result in big source-code headaches. It's also very easy to forget to properly connect Views together, which can result in errors in your layout and wasted time debugging your code. さっき終わらせた。"Hello, World"サンプルは、いわゆる"programmatic"なUIレイアウトだ。このことは、UI記述をソースコードに直接書いてビルドしているってことだ。UIプログラミングがおわっても、変更にもろいやりかただってことはわかるだろう。たとえば、ちょっとしたUIのレイアウトの変更が、大きなソースコードの変更につながったりとか。Viewクラス同士のつながりは忘れやすいし、それがデバッグに時間を浪費することにつながる。 //That's why Android provides an alternate UI construction model: XML-based layout files. The easiest way to explain this concept is to show an example. Here's an XML layout file that is identical in behavior to the programmatically-constructed example you just completed: そんなわけで、Androidでは、もうひとつのUI構築のモデルを提供している。それが、XMLベースのレイアウトファイルだ。このコンセプトを説明するには一例をあげるのが一番だね。ここに、今終わらせたプログラミングベースのものと同じ振る舞いをするXMLレイアウトファイルを用意しよう。 #highlight(XML){ <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Hello, Android"/> } //The general structure of an Android XML layout file is simple. It's a tree of tags, where each tag is the name of a View class. In this example, it's a very simple tree of one element, a TextView. You can use the name of any class that extends View as a tag name in your XML layouts, including custom View classes you define in your own code. This structure makes it very easy to quickly build up UIs, using a much simpler structure and syntax than you would in source code. This model is inspired by the web development model, where you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data. たいていの、Andorid XMLレイアウトファイルの構成はシンプルだ。タグのツリーからなっており、それぞれのタグは、Viewクラスの名前になっている。この例で言えば、TextView一要素だけからなる シンプルなツリー構成だ。XMLレイアウトファイルには、タグ名として、Viewクラスを継承したものなら、自作のものでも何でも使える。これは、Webの構築モデルからインスパイアされたものなんだ。ちょうど、UIの表示とデータを処理するアプリケーションロジックを分離できるみたいに。 //In this example, there are also four XML attributes. Here's a summary of what they mean: この例では、4つのXML属性がある。以下が、その意味の要約だ。 //|Attribute | Meaning |h |Attribute | 意味 |h //|xmlns:android | This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.| |xmlns:android | XMLネームスペース定義だ。これは、Androidネームスペースで定義された、共通の属性を参照するということをAndroidツールに知らせている。| //|android:layout_width | This attribute defines how much of the available width on the screen this View should consume. In this case, it's our only View so we want it to take up the entire screen, which is what a value of "fill_parent" means.| |android:layout_width | この要素は、このViewが消費する画面幅がどれくらいなのかを定義する要素だ。この場合で言えば、"fill_parent"を使っているが、画面全体の幅を指定しているってことになる。| //|android:layout_height | This is just like android:layout_width, except that it refers to available screen height. | |android:layout_height | android:layout_widthto同じようなものだが、これは高さを意味する。| //|android:text | This sets the text that the TextView should contain. In this example, it's our usual "Hello, Android" message. | |android:text | これは、TextViewの内容をセットするものだ。この例でいえば、いつもの"Hello, Android"だ。| //So, that's what the XML layout looks like, but where do you put it? Under the res/ directory in your project. The "res" is short for "resources" and that directory contains all the non-code assets that your application requires. This includes things like images, localized strings, and XML layout files. そう。XMLレイアウトはざっとこんな感じだ。けど、どうやってそれを組み込むと思う? resディレクトリの下に入れればOKだ。"res"は"resources"をはしょったもので、そのディレクトリには、アプリケーションに必要なコード以外の一式を詰め込んでおけばいい。たとえば、イメージや、ローカライズされた文字列や、XMLレイアウトファイルだ。 //The Eclipse plugin creates one of these XML files for you. In our example above, we simply never used it. In the Package Explorer, expand the folder res/layout, and edit the file main.xml. Replace its contents with the text above and save your changes. Eclipseプラグインは、XMLファイルを作成してくれる。上の例では単にそれを使わなかっただけだ。Package Explorerで、resフォルダの内容を開いて、main.xmlファイルに編集して、上のテキストをコピーして、変更を保存しよう。 //Now open the file named R.java in your source code folder in the Package Explorer. You'll see that it now looks something like this: Package Explorerのソースコードフォルダから、R.javaファイルを開いてみよう。次のようなものが表示されるはずだ。 #highlight(Java){{ public final class R { public static final class attr { }; public static final class drawable { public static final int icon=0x7f020000; }; public static final class layout { public static final int main=0x7f030000; }; public static final class string { public static final int app_name=0x7f040000; }; }; }} //A project's R.java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. This is particularly powerful with the code-completion features of IDEs like Eclipse because it lets you quickly and interactively locate the specific reference you're looking for. プロジェクトのR.javaファイルに全てのリソースにインデックスが定義されている。このクラスをソースコードのなかで、プロジェクトで使用しているリソースへの参照の速記方法の一つとしてつかえばいい。これはEclipseのようなコードコンプリート昨日を持つIDEでは特にパワフルだ。なぜなら、その昨日のおかげで、すばやく、インタラクティブに探しているリソース参照を配置することができるからだ。 //The important thing to notice for now is the inner class named "layout", and its member field "main". The Eclipse plugin noticed that you added a new XML layout file and then regenerated this R.java file. As you add other resources to your projects you'll see R.java change to keep up. この例で注意すべきは、"layouy"と命名されたインナークラスと、"main"と命名されたフィールドだ。新しいXMLレイアウトファイルを追加したら、エクリプスプラグインは通知し、R.javaファイルを再生成するだろう。つまり、他のリソースファイルをプロジェクトに追加するなら、R.javaファイルも更新されるのがわかるだろう。 //The last thing you need to do is modify your HelloAndroid source code to use the new XML version of your UI, instead of the hard-coded version. Here's what your new class will look like. As you can see, the source code becomes much simpler: 最後に、あなたのHelloAndroidコードを、ハードコーディングされたバージョンからXMLのUIを使うように修正する必要がある。新しいクラスはこのようになるはずだ。見ればわかるように、ソースコードはよりシンプルになった。 #highlight(Java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } } }} //When you make this change, don't just copy-and-paste it in. Try out the code-completion feature on that R class. You'll probably find that it helps a lot. この変更を加える際に、コピーアンドペーストをしないことだ。Rクラスに働くコードコンプリート機能を試してみよう。これがかなり使える機能だってことがわかるだろう。 //Now that you've made this change, go ahead and re-run your application — all you need to do is press the green Run arrow icon, or select Run > Run Last Launched from the menu. You should see.... well, exactly the same thing you saw before! After all, the point was to show that the two different layout approaches produce identical results. 変更を加えたら、アプリケーションを再起動してみよう。することと言えば、緑のRunと書かれた矢印アイコンをクリックし、Run -> Run Last Launchedをメニューから選択するだけだ。すると、さっき見たのと同じものが表示されるはずだ!結局のところ、2つの違ったレイアウトのアプローチは同じ結果をもたらすわけだ。 //There's a lot more to creating these XML layouts, but that's as far as we'll go here. Read the Implementing a User Interface documentation for more information on the power of this approach. XMLレイアウトの作成はもっと説明することがあるんだが、今ここで説明してもしょうがない。このアプローチのもっと詳細な情報のためには"Implementing a Uset Interface"ドキュメントを読んで欲しい。 //*Debugging Your Project *プロジェクトのデバッグ //The Android Plugin for Eclipse also has excellent integration with the Eclipse debugger. To demonstrate this, let's introduce a bug into our code. Change your HelloAndroid source code to look like this: EclipseのAndroidプラグインは、Eclipseデバッガにうまく統合されてもいる。それをデモするために、コードにバグを混入させてみよう。次のようにHelloAndroidのソースコードに変更を入れて欲しい。 #highlight(Java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Object o = null; o.toString(); setContentView(R.layout.main); } } }} //This change simply introduces a NullPointerException into your code. If you run your application again, you'll eventually see this: これは単に、NullPointerExceptionを入れてみただけだ。もう一度アプリケーションを実行させると、すぐに次のような画面を見るだろう。 #ref(hello_world_8.png); //To find out what went wrong, set a breakpoint in your source code on the line "Object o = null;" (You can do this by double-clicking on the region to the left of the line number in Eclipse.) Then select Run > Debug Last Launched from the menu to enter debug mode. Your app will restart in the emulator, but this time it will suspend when it reaches the breakpoint you set. You can then step through the code in Eclipse's Debug Perspective, just as you would for any other application. 何がおかしかったのかを特定するために、ソースコードの"Object o = null;"と書かれた行にブレークを貼ってみよう。(ブレークポイントを貼るためには、Eclipseの行番号の左の領域をダブルクリックすればいい)それから、Run -> Debug Last Launched を選択してデバッグモードに入ろう。エミュレータが再起動すると、セットしたブレークに到達した時点でアプリケーションは中断する。EclipseのDebug Perspectiveを通してどんなアプリケーションでもステップ実行ができる。 #ref(hello_world_9.png); //*Creating the Project without Eclipse *Eclipseなしでプロジェクトを作成する。 //If you don't use Eclipse (such as if you prefer another IDE, or simply use text editors and command line tools) then the Eclipse plugin can't help you. Don't worry though — you don't lose any functionality just because you don't use Eclipse. もし、あなたがEclipseを使用しないなら(たとえば別のIDEがいいだとか、そもそも単にテキストエディタとコマンドラインツールが使いたいなら)Eclipseプラグインはあなたの助けにはならないだろう。でも心配しなくていい。Eclipseを使用しないからといって、あなたは何も便利さを失わないのだ。 //The Android Plugin for Eclipse is really just a wrapper around a set of tools included with the Android SDK. (These tools, like the emulator, aapt, adb, ddms, and others are documented elsewhere.) Thus, it's possible to wrap those tools with another tool, such as an 'ant' build file. EclipseのAndroidプラグインは、Android SDKに同梱されているツール群のラッパーにすぎないからだ。(ツール群とは、エミュレータや、aapt,adb,ddmsなどだ。ほかにもどこかにドキュメントがあるだろう)それゆえ、他のツールを使って、それらをラッピングすることは可能だ。たとえば'ant'のビルドファイルを使うなどして。 //The Android SDK includes a Python script named "activityCreator.py" that can be used to create all the source code and directory stubs for your project, as well as an ant-compatible build.xml file. This allows you to build your project from the command line, or integrate it with the IDE of your choice. Android SDKは、"activityCreator.py"とネーミングされたPythonスクリプトも含んでいる。それは、antと互換性のあるbuild.xmlファイルはもちろんプロジェクトに必要なソースコードとスタブディレクトリを全て作成してくれる。これは、コマンドラインでプロジェクトをビルドできることと、あなたの好きな他のIDEと統合できることを意味する。 //For example, to create a HelloAndroid project similar to the one we just created via Eclipse, you'd use this command: たとえば、Eclipseを通してさっき作ったものと似たようなHelloAndroidプロジェクトを作成するためには、次のコマンドを使えばいい。 activityCreator.py --out HelloAndroid com.google.android.hello.HelloAndroid //To build the project, you'd then run the command 'ant'. When that command successfully completes, you'll be left with a file named HelloAndroid.apk under the 'bin' directory. That .apk file is an Android Package, and can be installed and run in your emulator using the 'adb' tool. プロジェクトをビルドするためには、'ant'コマンドを走らせればいい。コマンドが成功すれば、'bin'フォルダの下にHelloAndroid.apkと名づけられたがファイルがあるはずだ。この.apkファイルは、Android Packageで、'adb'ツールを使ってエミュレータにインストールして実行できる。 //For more information on how to use these tools, please read the documentation cited above. これらのツールについての使用方法をもっと知りたければ、上で紹介されたドキュメントを読んで欲しい。
*Hello, Android! 第一印象は重要だ。それは、あなたが、このアンドロイドというフレームワークを手にして、"Hello, World!"を書いたときに受ける第一印象だ。そう、アンドロイドにおいて、それはとても簡単なのだ。下記を見て欲しい。 **プロジェクトを作成する。 **UIを構築する。 **コードを走らせる: Hello, Android 以下のセクションでそれをつまびらかに語っていこう。 **UIをXMLのレイアウトにアップグレードする。 **プロジェクトをデバッグする。 **Eclipseなしでプロジェクトを作成する。 さあ行こう。 *プロジェクトを作成する プロジェクトを作成することはできる限り簡単にしてある。Eclipseプラグインで、Androidの開発環境のスナップを作成することが可能だ。[[Eclipse 3.3以上(Europa)>http://eclipse.org/]]と、[[Eclipse用のAndroidプラグイン>http://www29.atwiki.jp/android/pages/14.html#id_0f2eb0b7]]は用意してあるかい?それらをインストールしてから次に進んで欲しい。 最初に、"Hello, World!"をビルドするための、簡単な要約を述べておこう。 +File->New->Project menuから、"Android Project"を新しく作成する。 +New Android Project ダイアログで、プロジェクトの詳細を埋める。 +なにかを表示するための自動生成されたテンプレートコードを編集する。 さあ、行こう!以下でそれぞれのステップの詳細を説明しよう。 **1.新しい"Android Project"を作成する。 Eclipseから、File->New->Projectと選択して欲しい。もし、EclipseのAndroidプラグインがきちんとインストールされているなら、表示されるダイアログの中に、"Android"と名前のついたフォルダがあり、その中には、"Android Project"があるはずだ。 #ref(hello_world_0.png); "Android Project"を選択し、"Next"を押そう。 **2.New Android Project ダイアログで、プロジェクトの詳細を埋める。 次の画面で、プロジェクトに関係する詳細を入力する。たとえば次の例のように: #ref(hello_world_1.png); それぞれの入力欄が意味するところは次のようになる。 |Project Name|プロジェクトを保存したいディレクトリもしくはフォルダの名前| |Package Name|これはパッケージの名前空間だ。ちょうどJavaのように。あなたのソースコードは全てここより下位におかれるようにする。ここには、自動生成されたスタブのパッケージ名がすでにセットされているはずだ。パッケージ名は、システムにインストールされるすべてのパッケージ間で、ユニークである必要がる。というわけで、あなたのアプリケーションに標準的なドメイン命名スタイルを使うことはとても重要だ。上述の例では、パッケージ名として、ドメイン"com.google.android"を使用している。あなたの所属する組織にみあった、唯一の名前を使用するといいだろう。| |Activity Name|ここでは、プラグインによって生成されるスタブクラスの名前が書かれている。これは、AndroidのActivityクラスのサブクラスである。Activityは単純なクラスで、それ自体で実行させ、処理させることができる。希望するならUIも作れるが、そうしなくても構わない| |Application Name|ここにはユーザーが目にするアプリケーションのタイトルを入力する。| "Use default location"チェックボックスをONにすることで、プロジェクトファイルの保存場所を変更することができる。 **3.自動生成コードを編集する。 After the plugin runs, you'll have a class named HelloAndroid that looks like this: プラグインを実行すると、下記のような、HelloAndroidクラスが出来上がっているがわかるだろう。 #highlight(java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } } }} 次のステップで、これを修正していこう! *UIを構築する。 プロジェクトをセットアップしたあとは、当然、それを修正していく。以下がその完成品だ。1行ずつ解剖していこう。 #highlight(java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } } }} このサンプルのコンパイルをするために、インポートセクションに、"import android.widget.TextView;"を追加するしなければらいことを注意しよう。 Androidでは、ユーザーインターフェースは、Viewsと呼ばれるクラス階層で成り立っている。Viewはシンプルな描画オブジェクトである。たとえば、ラジオボタンであるとか、アニメーションであるとか、(今回のケースは)テキストラベルなどだ。テキストを扱えるVieewのサブクラスの名前は、単に、TextViewとなる。 下記が、TextViewクラスを生成する方法だ。 #highlight(java){ TextView tv = new TextView(this); } TextViewクラスのコンストラクタに渡している引数は、Android Contextのインスタンスである。Contextは単にシステムへ渡すハンドルである。そのハンドルは、リソースを解決したり、データベースや設定などにアクセスするために供給されている。ActivityクラスはContextから派生している。それゆえ、HelloAndroidクラスはActivityクラスのサブクラスであり、コンテキストであるのだ。だから、"this"参照をTextViewに渡すことができる。 一度TextViewを生成してしまえば、何を表示するのか伝えてあげる必要がある。 #highlight(java){ tv.setText("Hello, Android"); } とくに特筆すべきことはないだろう。 ここまでで、TextViewを生成し、どんなテキストをディスプレイに表示すべきかを伝えた。最後のステップは、実際のディスプレイに、TextViewをつなぐことである。こんな感じに。 #highlight(java){ setContentView(tv); } ActivityのsetContentViewメソッドは、ActivityのUIにどのViewが関連付けられるべきかをシステムに通知する。もし、Activityがこのメソッドをコールしないなら、UIは何も表示されないし、システムは真っ白けの画面を表示することだろう。今のところの目的は、何でもいいからテキストを表示することなので、作ったばかりのTextViewを渡してしまえばよい。 これで、アンドロイドでの"Hello, World"のコーディングは完了だ。もちろん、つぎは、実行させるてみよう。 *コードの実行:Hello, Android Eclipseプラグインのおかげで、とても簡単にあなたのアプリケーションを実行することができる。メニューからRunを選択すると、下のようなダイアログが表示される。 #ref(hello_world_2.png); 次に、"Android Application"を選択しよう。そして、アイコンの左上をクリックしよう(+印とともに、画面に描画されているやつだ)。それとも、単に、"Android Application"をダブルクリックするだけでいい。"New_configuration"と名づけられた新しいランチャーダイアログが表示されるはずだ。 #ref(hello_world_3.png); 名前を何か適当なもの、たとえば、"Hello, Android"と変更して、"Browse"ボタンを押下して、あなたのプロジェクトを選択しよう。(もしあなたが2個以上のAndroidプロジェクトをEclipseで開いていたら、正しいものを選択しているかどうか確かめてほしい)プラグインは、自動的に、あなたのプロジェクトからActivityのサブクラスをスキャンして、"Activity:"ラベルの下のドロップダウンリストに追加してくれる。デフォルトでは、あなたは、"Hello, Android"プロジェクトしか作っていないから、単に続けるだけでいい。 "Apply"ボタンを押下しよう。こういう風になる。 #ref(hello_world_4.png); これで成功だ。"Run"ボタンを押してみよう。Androidエミュレータがスタートするはずだ。起動完了したら、あなたのアプリケーションが表示されるだろう。今までいったことが全部できていれば、次のような画面を目にすることができるはずだ。 #ref(hello_world_5.png); これが、Androidにおける、"Hello, World"だ。とっても簡単だったろう?チュートリアルの次のセクションでは、Androidについて、より詳細な価値ある情報を知ることができるだろう。 //*Upgrading the UI to an XML Layout *UIをXMLレイアウトにアップグレードする。 //The "Hello, World" example you just completed uses what we call "programmatic" UI layout. This means that you construct and build your application's UI directly in source code. If you've done much UI programming, you're probably familiar with how brittle that approach can sometimes be: small changes in layout can result in big source-code headaches. It's also very easy to forget to properly connect Views together, which can result in errors in your layout and wasted time debugging your code. さっき終わらせた。"Hello, World"サンプルは、いわゆる"programmatic"なUIレイアウトだ。このことは、UI記述をソースコードに直接書いてビルドしているってことだ。UIプログラミングがおわっても、変更にもろいやりかただってことはわかるだろう。たとえば、ちょっとしたUIのレイアウトの変更が、大きなソースコードの変更につながったりとか。Viewクラス同士のつながりは忘れやすいし、それがデバッグに時間を浪費することにつながる。 //That's why Android provides an alternate UI construction model: XML-based layout files. The easiest way to explain this concept is to show an example. Here's an XML layout file that is identical in behavior to the programmatically-constructed example you just completed: そんなわけで、Androidでは、もうひとつのUI構築のモデルを提供している。それが、XMLベースのレイアウトファイルだ。このコンセプトを説明するには一例をあげるのが一番だね。ここに、今終わらせたプログラミングベースのものと同じ振る舞いをするXMLレイアウトファイルを用意しよう。 #highlight(XML){ <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Hello, Android"/> } //The general structure of an Android XML layout file is simple. It's a tree of tags, where each tag is the name of a View class. In this example, it's a very simple tree of one element, a TextView. You can use the name of any class that extends View as a tag name in your XML layouts, including custom View classes you define in your own code. This structure makes it very easy to quickly build up UIs, using a much simpler structure and syntax than you would in source code. This model is inspired by the web development model, where you can separate the presentation of your application (its UI) from the application logic used to fetch and fill in data. たいていの、Andorid XMLレイアウトファイルの構成はシンプルだ。タグのツリーからなっており、それぞれのタグは、Viewクラスの名前になっている。この例で言えば、TextView一要素だけからなる シンプルなツリー構成だ。XMLレイアウトファイルには、タグ名として、Viewクラスを継承したものなら、自作のものでも何でも使える。これは、Webの構築モデルからインスパイアされたものなんだ。ちょうど、UIの表示とデータを処理するアプリケーションロジックを分離できるみたいに。 //In this example, there are also four XML attributes. Here's a summary of what they mean: この例では、4つのXML属性がある。以下が、その意味の要約だ。 //|Attribute | Meaning |h |Attribute | 意味 |h //|xmlns:android | This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.| |xmlns:android | XMLネームスペース定義だ。これは、Androidネームスペースで定義された、共通の属性を参照するということをAndroidツールに知らせている。| //|android:layout_width | This attribute defines how much of the available width on the screen this View should consume. In this case, it's our only View so we want it to take up the entire screen, which is what a value of "fill_parent" means.| |android:layout_width | この要素は、このViewが消費する画面幅がどれくらいなのかを定義する要素だ。この場合で言えば、"fill_parent"を使っているが、画面全体の幅を指定しているってことになる。| //|android:layout_height | This is just like android:layout_width, except that it refers to available screen height. | |android:layout_height | android:layout_widthto同じようなものだが、これは高さを意味する。| //|android:text | This sets the text that the TextView should contain. In this example, it's our usual "Hello, Android" message. | |android:text | これは、TextViewの内容をセットするものだ。この例でいえば、いつもの"Hello, Android"だ。| //So, that's what the XML layout looks like, but where do you put it? Under the res/ directory in your project. The "res" is short for "resources" and that directory contains all the non-code assets that your application requires. This includes things like images, localized strings, and XML layout files. そう。XMLレイアウトはざっとこんな感じだ。けど、どうやってそれを組み込むと思う? resディレクトリの下に入れればOKだ。"res"は"resources"をはしょったもので、そのディレクトリには、アプリケーションに必要なコード以外の一式を詰め込んでおけばいい。たとえば、イメージや、ローカライズされた文字列や、XMLレイアウトファイルだ。 //The Eclipse plugin creates one of these XML files for you. In our example above, we simply never used it. In the Package Explorer, expand the folder res/layout, and edit the file main.xml. Replace its contents with the text above and save your changes. Eclipseプラグインは、XMLファイルを作成してくれる。上の例では単にそれを使わなかっただけだ。Package Explorerで、resフォルダの内容を開いて、main.xmlファイルに編集して、上のテキストをコピーして、変更を保存しよう。 //Now open the file named R.java in your source code folder in the Package Explorer. You'll see that it now looks something like this: Package Explorerのソースコードフォルダから、R.javaファイルを開いてみよう。次のようなものが表示されるはずだ。 #highlight(Java){{ public final class R { public static final class attr { }; public static final class drawable { public static final int icon=0x7f020000; }; public static final class layout { public static final int main=0x7f030000; }; public static final class string { public static final int app_name=0x7f040000; }; }; }} //A project's R.java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. This is particularly powerful with the code-completion features of IDEs like Eclipse because it lets you quickly and interactively locate the specific reference you're looking for. プロジェクトのR.javaファイルに全てのリソースにインデックスが定義されている。このクラスをソースコードのなかで、プロジェクトで使用しているリソースへの参照の速記方法の一つとしてつかえばいい。これはEclipseのようなコードコンプリート機能を持つIDEでは特にパワフルだ。なぜなら、その機能のおかげで、すばやく、インタラクティブに探しているリソース参照を配置することができるからだ。 //The important thing to notice for now is the inner class named "layout", and its member field "main". The Eclipse plugin noticed that you added a new XML layout file and then regenerated this R.java file. As you add other resources to your projects you'll see R.java change to keep up. この例で注意すべきは、"layout"と命名されたインナークラスと、"main"と命名されたフィールドだ。新しいXMLレイアウトファイルを追加したら、エクリプスプラグインは通知し、R.javaファイルを再生成するだろう。つまり、他のリソースファイルをプロジェクトに追加するなら、R.javaファイルも更新されるのがわかるだろう。 //The last thing you need to do is modify your HelloAndroid source code to use the new XML version of your UI, instead of the hard-coded version. Here's what your new class will look like. As you can see, the source code becomes much simpler: 最後に、あなたのHelloAndroidコードを、ハードコーディングされたバージョンからXMLのUIを使うように修正する必要がある。新しいクラスはこのようになるはずだ。見ればわかるように、ソースコードはよりシンプルになった。 #highlight(Java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } } }} //When you make this change, don't just copy-and-paste it in. Try out the code-completion feature on that R class. You'll probably find that it helps a lot. この変更を加える際に、コピーアンドペーストをしないことだ。Rクラスに働くコードコンプリート機能を試してみよう。これがかなり使える機能だってことがわかるだろう。 //Now that you've made this change, go ahead and re-run your application — all you need to do is press the green Run arrow icon, or select Run > Run Last Launched from the menu. You should see.... well, exactly the same thing you saw before! After all, the point was to show that the two different layout approaches produce identical results. 変更を加えたら、アプリケーションを再起動してみよう。することと言えば、緑のRunと書かれた矢印アイコンをクリックし、Run -> Run Last Launchedをメニューから選択するだけだ。すると、さっき見たのと同じものが表示されるはずだ!結局のところ、2つの違ったレイアウトのアプローチは同じ結果をもたらすわけだ。 //There's a lot more to creating these XML layouts, but that's as far as we'll go here. Read the Implementing a User Interface documentation for more information on the power of this approach. XMLレイアウトの作成はもっと説明することがあるんだが、今ここで説明してもしょうがない。このアプローチのもっと詳細な情報のためには"Implementing a Uset Interface"ドキュメントを読んで欲しい。 //*Debugging Your Project *プロジェクトのデバッグ //The Android Plugin for Eclipse also has excellent integration with the Eclipse debugger. To demonstrate this, let's introduce a bug into our code. Change your HelloAndroid source code to look like this: EclipseのAndroidプラグインは、Eclipseデバッガにうまく統合されてもいる。それをデモするために、コードにバグを混入させてみよう。次のようにHelloAndroidのソースコードに変更を入れて欲しい。 #highlight(Java){{ public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Object o = null; o.toString(); setContentView(R.layout.main); } } }} //This change simply introduces a NullPointerException into your code. If you run your application again, you'll eventually see this: これは単に、NullPointerExceptionを入れてみただけだ。もう一度アプリケーションを実行させると、すぐに次のような画面を見るだろう。 #ref(hello_world_8.png); //To find out what went wrong, set a breakpoint in your source code on the line "Object o = null;" (You can do this by double-clicking on the region to the left of the line number in Eclipse.) Then select Run > Debug Last Launched from the menu to enter debug mode. Your app will restart in the emulator, but this time it will suspend when it reaches the breakpoint you set. You can then step through the code in Eclipse's Debug Perspective, just as you would for any other application. 何がおかしかったのかを特定するために、ソースコードの"Object o = null;"と書かれた行にブレークを貼ってみよう。(ブレークポイントを貼るためには、Eclipseの行番号の左の領域をダブルクリックすればいい)それから、Run -> Debug Last Launched を選択してデバッグモードに入ろう。エミュレータが再起動すると、セットしたブレークに到達した時点でアプリケーションは中断する。EclipseのDebug Perspectiveを通してどんなアプリケーションでもステップ実行ができる。 #ref(hello_world_9.png); //*Creating the Project without Eclipse *Eclipseなしでプロジェクトを作成する。 //If you don't use Eclipse (such as if you prefer another IDE, or simply use text editors and command line tools) then the Eclipse plugin can't help you. Don't worry though — you don't lose any functionality just because you don't use Eclipse. もし、あなたがEclipseを使用しないなら(たとえば別のIDEがいいだとか、そもそも単にテキストエディタとコマンドラインツールが使いたいなら)Eclipseプラグインはあなたの助けにはならないだろう。でも心配しなくていい。Eclipseを使用しないからといって、あなたは何も便利さを失わないのだ。 //The Android Plugin for Eclipse is really just a wrapper around a set of tools included with the Android SDK. (These tools, like the emulator, aapt, adb, ddms, and others are documented elsewhere.) Thus, it's possible to wrap those tools with another tool, such as an 'ant' build file. EclipseのAndroidプラグインは、Android SDKに同梱されているツール群のラッパーにすぎないからだ。(ツール群とは、エミュレータや、aapt,adb,ddmsなどだ。ほかにもどこかにドキュメントがあるだろう)それゆえ、他のツールを使って、それらをラッピングすることは可能だ。たとえば'ant'のビルドファイルを使うなどして。 //The Android SDK includes a Python script named "activityCreator.py" that can be used to create all the source code and directory stubs for your project, as well as an ant-compatible build.xml file. This allows you to build your project from the command line, or integrate it with the IDE of your choice. Android SDKは、"activityCreator.py"とネーミングされたPythonスクリプトも含んでいる。それは、antと互換性のあるbuild.xmlファイルはもちろんプロジェクトに必要なソースコードとスタブディレクトリを全て作成してくれる。これは、コマンドラインでプロジェクトをビルドできることと、あなたの好きな他のIDEと統合できることを意味する。 //For example, to create a HelloAndroid project similar to the one we just created via Eclipse, you'd use this command: たとえば、Eclipseを通してさっき作ったものと似たようなHelloAndroidプロジェクトを作成するためには、次のコマンドを使えばいい。 activityCreator.py --out HelloAndroid com.google.android.hello.HelloAndroid //To build the project, you'd then run the command 'ant'. When that command successfully completes, you'll be left with a file named HelloAndroid.apk under the 'bin' directory. That .apk file is an Android Package, and can be installed and run in your emulator using the 'adb' tool. プロジェクトをビルドするためには、'ant'コマンドを走らせればいい。コマンドが成功すれば、'bin'フォルダの下にHelloAndroid.apkと名づけられたがファイルがあるはずだ。この.apkファイルは、Android Packageで、'adb'ツールを使ってエミュレータにインストールして実行できる。 //For more information on how to use these tools, please read the documentation cited above. これらのツールについての使用方法をもっと知りたければ、上で紹介されたドキュメントを読んで欲しい。

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。