Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發常見錯誤與解法大全

編輯:關於 Android 程式設計

Android 開發過程中難免遇到各種錯誤,以下整理一系列常見的報錯訊息與對應的解決方式,方便隨時查閱。

1. Unable to resolve target 'android-2'

安裝對應的 API 版本,或在 default.properties 中把 target=android-2 改為 target=android-7 即可。

2. Invalid start tag LinearLayout

main.xml 放錯資料夾了,應該放在 \res\layout 下。

3. INSTALL_FAILED_INSUFFICIENT_STORAGE

原因:SD 卡容量不足。解法一是在模擬器的「Additional Emulator Command Line Options」中加上 -partition-size 128;解法二是進入 settings > applications > manage applications,選>That 應用並 uninstall 徹底刪除後再重裝。

4. No Space left on device

清除 C:\Users\<使用者>\AppData\Local\Temp\AndroidEmulator 資料夾下的 tmp 檔。

5. Resource is out of sync with the file system

右鍵點擊專案 Refresh 一下即可。原因通常是檔案在 Eclipse 之外被編輯了。

6. Invalid project description

該工程已被導入 Eclipse,一個工程不能重複導入兩次;或把資料夾複製到別的目錄後重新導入即可。

7. java.lang.SecurityException: Requires VIBRATE permission

在 AndroidManifest.xml 加上 <uses-permission android:name="android.permission.VIBRATE"></uses-permission>。原因是使用了震動功能卻未宣告權限。

8. Activity not started, its current task has been brought to the front

在 Eclipse 中執行「Project」→「Clean…」。原因是模擬器中還有東西在運行,該 Activity 已有一個實例存在。

9. ArrayAdapter requires the resource ID to be a TextView

new ArrayAdapter 的第二個參數必須是 TextView 的資源 ID,可改為 android.R.layout.simple_spinner_dropdown_item。

10. R.java 突然不見了

右鍵專案 → Build Project。原因通常是 res/drawable 下的檔名含有 a~z、0~9 以外的字元。

11. ScrollView can host only one direct child

ScrollView 內部只能有一個直接子元素,需把所有子元素包進一個 LinearLayout 或 RelativeLayout 中。

12. the user data image is used by another emulator

這是沒有正常關閉導致的,刪除 .android\avd\<版本> 下以 .lock 結尾的資料夾即可,因為這些鎖檔未被釋放。

13. ADB server didn't ACK / failed to start daemon

在工作管理員中關閉所有 adb.exe,然後重啟 Eclipse 即可。

14. Unable to instantiate activity ComponentInfo

將 android:name="Acitivity" 改為 android:name=".Acitivity",即 Manifest 中 Activity 的路徑寫錯了。

15. 工程名上出現紅色感嘆號

右鍵 Build Path → Configure Build Path,在 Libraries 中找到帶叉號的套件(路徑錯誤),修正後 F5 刷新。通常是 .classpath 引用了實際上不存在的 jar。

16. ActivityNotFoundException

使用 Intent 時找不到目標 Activity,需在 AndroidManifest.xml 中補上對應的 <activity> 設定。

17. Fail to connect to camera service

在 Manifest 加入 <uses-permission android:name="android.permission.CAMERA" />;若仍不行,在 surfaceDestroyed 中加上 mCamera.stopPreview(); mCamera.release(); 正確釋放相機。

18. WindowManager$BadTokenException: token null is not for an application

建立 AlertDialog 時必須傳入 Activity 的 Context(如 Activity.this),而不能用 getApplicationContext(),因为只有 Activity 才能添加窗體。

19. Circular dependencies cannot exist in RelativeLayout

兩個互相依賴的位置屬性不能同時使用,需調整 RelativeLayout 的對齊屬性避免循環依賴。

20. No resource found that matches the given name

在相對佈局中若用到 android:layout_above 且其 id 之前未出現過,需寫成 @+id/...,多加一個「+」。

21. @Override 錯誤

Window → Preferences → Java → Compiler,把 Compiler compliance level 改為 1.6,且不要在個別專案裡做 specific 覆寫。

22. Activity has leaked window that was originally added

原因一是 onKeyDown 中 return super 未改;原因二是關閉 Activity 時沒有關閉 AlertDialog。可將 return 改為 false,或在 onPause 中 dismiss 對話框,或在 Manifest 加上 android:configChanges="orientation|keyboardHidden|navigation"。

23. unable to instantiate application

可嘗試:移除 Manifest 中 application 的 android:name;將全局類或變量改為私有;或檢查 package 路徑是否正確。

24. IllegalArgumentException 'Provider "gps" unknown'

呼叫 setTestProviderEnabled 前,需先用 addTestProvider 註冊該測試提供者並設定各項參數即可。

以上就是 Android 開發中經常遇到的錯誤整理。遇到問題時,先看懂錯誤訊息再對照排查,往往能事半功倍。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved