Android 開發過程中常遇到的經典錯誤與解決方案整理,涵蓋權限配置、Activity 宣告、API 版本差異、Layout 載入、建置錯誤、Intent 參數混淆、SD 卡寫入權限、UI 佈局技巧、軟鍵盤處理、ScrollView 使用等 11 大類問題。
在 AndroidManifest.xml 的 </application> 之後添加:
<uses-permission android:name="android.permission.INTERNET" />
在 Manifest 中宣告 Activity:
<activity android:name=".xxxActivity"> </activity>
已棄用,請改用 startActivityForResult()
忘記在 Activity 中載入 Layout:
setContentView(R.layout.main);
執行選單:Project → Clean 通常可解決
startActivityForResult(Intent, requestCode):requestCode ≥ 0,用於識別回傳來源setResult(resultCode, Intent):resultCode 識別執行結果(常用 RESULT_OK)onActivityResult(requestCode, resultCode, Intent):三參數對應處理Manifest 添加權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:layout_gravity="center_vertical"
使用 RelativeLayout,左側控件預設靠左,右側控件設 android:layout_alignParentRight="true"
<ImageView android:layout_alignParentRight="true" ... />
在 Manifest 對應 Activity 添加:
android:windowSoftInputMode="adjustPan"
或固定佈局高度避免被壓縮
用 <ScrollView> 包裹原有佈局:
<ScrollView xmlns:android="..."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
...原有佈局...
</ScrollView>