2009年5月30日 星期六

window環境下載Android source code

今天寫程式的時候突然想看一下Android的原始碼長怎樣
因為在寫ListActivity類別的時候對他的架構很好奇,但是SDK裡面的android.jar並沒有.java檔,所以找了一下windows下載原始碼的方法

首先要有下載工具,請到下列網址下載git,c


http://code.google.com/p/msysgit/downloads/list
直接下載(可能因為版本更新而失效)
Git-1.6.3-preview20090507-2.exe


之後到Android source網站
http://android.git.kernel.org/
裡面有Linux直接下載的方法,window要下載必須用上面的工具
上面的工具下載下來安裝後,執行Git Bash



之後會出現類似cmd的畫面,要下載source code,就輸入

git clone git://android.git.kernel.org/ + project path.


project path參照http://android.git.kernel.org/
請照自己的需要下載,整個Android原始碼2G多
舉個例子,我只想看android開發的相關類別像是TextView等等
我就只須要下載platform/frameworks/base.git,Android的framework都放在這
輸入

git clone git://android.git.kernel.org/platform/frameworks/base.git

就會開始下載了,之後就可以遨遊Android的世界

2009年5月29日 星期五

Log配合logcat

在Android除錯最常用的方式就是開Logcat來觀看,他可以取代JRE的Console來幫忙偵錯
不然每次程式錯誤都要寫個AlertDialog也是個麻煩
LogCat叫出方法,Window -> Show View -> Other,找Android -> LogCat

而在Android有個Log類別可以跟Logcat配合,他可以幫忙除錯,有點像以前我們寫C/C++用土法煉鋼printf來看資訊一樣

Log基本用法

Log.類型(標籤名子,訊息)

Log的類型大概分以下幾類
  1. Log.v(TAG,Message) :Verbose 記錄詳細訊息
  2. Log.d(TAG,Message) :Debug 除錯
  3. Log.i(TAG,Message) :INFO 資訊
  4. Log.w(TAG,Message):Warning 警告
  5. Log.e(TAG,Message) : Error 錯誤

------------
其中TAG是可以幫助我們篩選訊息,因為一般logcat會擷取一大堆訊息,包括GC做了什麼
自定Tag可以幫我們下Filter

LogCat雜七雜八訊息

而我加入了自己的訊息,假設我想記錄開啟程式成功與否

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Log.i("Create Android", "Test");
findVuew();
setClick();


}

而我想過濾那些雜七雜八的訊息,可以從右上的綠色加號增加filter

之後會跳出設定視窗

裡面幾個必要參數
  1. Filter Name:過濾名稱 可以隨自己喜好取
  2. by Log Tag:想留下的Tag名稱,在此設我剛剛取的Create Android
  3. by Log level :這選項是可選的,可以過濾Log的類型,在此我選none


而接下來,就只會剩下我想看到的訊息

Android一些資源站

在開發程式的時候總是希望自己的程式美美的
尤其是在講就豐富使用者介面的現在,更是須要美化程式界面的方法
從書上看到的一些資源站,主要是各種免費且漂亮的界面圖片、icon等等

Tango
http://tango.freedesktop.org/Tango_Icon_Library#Download

Nuvola
http://www.icon-king.com/projects/nuvola/

openclipart
http://openclipart.org/media/view/media/developer

使用findViewById傳回Null及Android重構

今天在寫Android的時候,使用findViewById尋找layout上的物件
原本好好的,但是想不到重構後變成不會跑了,後來用logcat看了一下
原來是findViewById回傳了null的物件,結果丟出NullPointerException
重構前程式

private EditText _width;
private EditText _height;
...
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_width=(EditText)findViewById(R.id.width_text);
_height=(EditText)findViewById(R.id.height_text);
}

重構後程式

private EditText _width;
private EditText _height;
...
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
findView();
setContentView(R.layout.main);

}
private void findView(){
_width=(EditText)findViewById(R.id.width_text);
_height=(EditText)findViewById(R.id.height_text);
}

------------
原來是我把findViewById的動作移到了 setContentView(R.layout.main);之前
在Android如果在使用setContentView把顯示元件創造出來之前
findViewById是找不到任何東西的,所以必須把findViewById的動作放到setContentView之後


這邊順便介紹一下,在Android裡有提供一個好用的重構工具:Extract Android String
在Android通常會把常數字串放到string.xml裡面存取,而當我們寫程式的時候通常很懶的再把東西寫到另外一個檔案裡面,這個工具可以簡單替我們辦到
而這項重構工具在Refactor->Android->Extract Android String




順便筆記一下,如果想存取string.xml,也就是R.string之下的東西,必須配合getText函式

_result.setText(getText(R.string.data);