Creating an Activity with intent
[Intent Objects]
•用途:在兩個Activity之間跳轉時使用了Intent.
•An Intent object is a bundle of information.
•It contains information of interest to the component that receives the intent
•Intent messaging is a facility for late run-time binding between components in the same or different applications.
[Action & data ]
The primary pieces of information in an intent:
–action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN.
–data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
[Intent type]
•Explicit Intents have specified a component
–via setComponent(ComponentName) or setClass(Context, Class))
–用setClass來設置Intent的發起方與接收方.
•Implicit Intents have not specified a component;
–instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
–不需要用setClass或setComponent來指定事件處理器,利用AndroidMenifest.xml中的配置就可以由平臺定位事件的消費者。
•intent要定位事件的目的地,需要以下幾個資訊:
1.Category:比如我們常見的 LAUNCHER_CATEGORY 就是表示這是一類應用程式。
2.Type:在前面的例子中沒用過,表示資料的類型,這是隱性Intent定位目標的重要依據。
3.Component:前面的例子中用的是setClass,不過也可以用setComponent來設置intent跳轉的前後兩個類實例。
4.Extras:在ContentURI之外還可以附加一些資訊,它是Bundle類型的物件。
[程式練習]
學了intent後,當然要寫一個程式來試試看啦!
我的構想如下:

好不容易照著書上寫的把intent程式打好,再改寫程式加入上述電子虎的構想
但馬上就遇到錯誤:
[Error1:AndroidManifest.xml的功用?]
出現這個錯誤訊息:
•android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.electronictiger/com.example.electronictiger.Tiger}; have you declared this activity in your AndroidManifest.xml?
•java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.electronictiger/com.example.electronictiger.Tiger}: java.lang.NullPointerException


完成這程式後還是有許多疑問:
問題一: 這是顯性還隱性INTENT?
問題二: Bundle是什麼?如何用?
•用途:在兩個Activity之間跳轉時使用了Intent.
•An Intent object is a bundle of information.
•It contains information of interest to the component that receives the intent
•Intent messaging is a facility for late run-time binding between components in the same or different applications.
[Action & data ]
The primary pieces of information in an intent:
–action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN.
–data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
[Intent type]
•Explicit Intents have specified a component
–via setComponent(ComponentName) or setClass(Context, Class))
–用setClass來設置Intent的發起方與接收方.
•Implicit Intents have not specified a component;
–instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
–不需要用setClass或setComponent來指定事件處理器,利用AndroidMenifest.xml中的配置就可以由平臺定位事件的消費者。
•intent要定位事件的目的地,需要以下幾個資訊:
1.Category:比如我們常見的 LAUNCHER_CATEGORY 就是表示這是一類應用程式。
2.Type:在前面的例子中沒用過,表示資料的類型,這是隱性Intent定位目標的重要依據。
3.Component:前面的例子中用的是setClass,不過也可以用setComponent來設置intent跳轉的前後兩個類實例。
4.Extras:在ContentURI之外還可以附加一些資訊,它是Bundle類型的物件。
[程式練習]
學了intent後,當然要寫一個程式來試試看啦!
我的構想如下:

好不容易照著書上寫的把intent程式打好,再改寫程式加入上述電子虎的構想
但馬上就遇到錯誤:
[Error1:AndroidManifest.xml的功用?]
出現這個錯誤訊息:
•android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.electronictiger/com.example.electronictiger.Tiger}; have you declared this activity in your AndroidManifest.xml?
•java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.electronictiger/com.example.electronictiger.Tiger}: java.lang.NullPointerException
原來是activity name錯誤,修正之:
[Error2: TextView issue]
又遇到另一個問題,錯在第64行,: 呼叫setText()就會error.但始終找不到原因....
經過一番debug後,原來是layout出問題, tiger.xml 內的TextView android:id打字錯誤.原來Eclipse有些錯誤並不會告知.
修正過後的layout
程式flow與總結,目前這只是intent的練習,未來會再加入更多detail.ex:圖片,AI...

完成這程式後還是有許多疑問:
問題一: 這是顯性還隱性INTENT?
問題二: Bundle是什麼?如何用?
留言
張貼留言