我的android應用程序顯示keydispatching超時錯誤。每當我觸摸屏幕時,我都不會發生。我發生應用程序啓動幾秒鐘後發生。這將是什麼原因?我使用了4個imageviews,oncreate()。當我觸摸圖像視圖時,它必須通過GPRS從服務器下載圖像。我在onCreate()方法中實現了主線程的完整操作。這是我的探頭嗎?我應該實現onStart(),onResume(),onPause(),onRestart()和onDestroy()方法嗎?android顯示錯誤
回答
你不能做加工是需要時間的任何顯着的程度(比如下載的東西從網絡中)在UI線程中,這是所有這些方法都被調用的地方。
您必須將這部分功能移到服務中或在循環線程中執行。
請不要爲此使用服務。使用AsyncTask http://developer.android.com/intl/de/reference/android/os/AsyncTask.html – Janusz 2010-10-22 14:34:30
我會在這方面向你推薦。我的主要觀點是讓它脫離UI線程。 – 2010-10-22 22:57:18
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);
dlgAlert = new AlertDialog.Builder(this);
// location = new Location(provider);
imView = (ImageView)findViewById(R.id.mapview);
zoomInView = (ImageView)findViewById(R.id.zoomin);
zoomOutView = (ImageView)findViewById(R.id.zoomout);
exitView = (ImageView)findViewById(R.id.exit);
imView.setOnTouchListener(新View.OnTouchListener(){ 公共布爾onTouch(查看視圖,MotionEvent事件){ 如果(event.getAction()== MotionEvent.ACTION_DOWN){// 爲opertions向下運動 }否則,如果(event.getAction()== MotionEvent.ACTION_UP){// 運營跟進行動 }
我相信你會得到ANR(App Not Responding)消息。
我建議你做一個單獨的線程下載圖像。使用的AsyncTask做這樣的下載任務,主線程的onCreate()&避免下載數據或任何其他生命週期方法
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i/(float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
檢查鏈接http://developer.android.com/reference/android/os/AsyncTask.html
- 1. 顯示錯誤! Android
- 2. TimePicker在Android顯示錯誤
- 3. 錯誤顯示的Android
- 4. Android ImageButton顯示錯誤
- 5. android,Manifest爲android顯示錯誤:targetSdkVersion
- 6. 顯示錯誤
- 7. Android Studio沒有顯示任何錯誤?
- 8. android:視頻水印顯示錯誤
- 9. 錯誤:顯示java.lang.NullPointerException在android系統
- 10. 的Android Studio現在顯示此錯誤
- 11. Android的DatePicker值顯示錯誤的值
- 12. Eclipse沒有顯示Android的錯誤
- 13. 錯誤時顯示在android系統
- 14. eclipse在android項目上顯示錯誤
- 15. Android版YouTube阿比顯示錯誤
- 16. Android Stack Trace - 在哪裏顯示錯誤?
- 17. Android ListView顯示錯誤的位置
- 18. android通過intent顯示parseobject的錯誤
- 19. Android的Webview與JavaScript其顯示錯誤
- 20. Android與線程顯示錯誤
- 21. 錯誤的Android火力顯示java.lang.NullPointerException
- 22. Android Studio Logcat不顯示錯誤
- 23. RecycleView突出顯示錯誤行(Android)
- 24. Android程序中的AlertDialog.Builder顯示錯誤
- 25. Android Lint不顯示翻譯錯誤錯誤
- 26. 的Android Studio 2.2中顯示錯誤:CreateProcess的錯誤= 216
- 27. HTML顯示錯誤
- 28. JavaScript錯誤顯示
- 29. Patters顯示錯誤
- 30. Fancybox顯示錯誤
你可以做至少兩件事情得到一些結果:後詳細info(日誌,代碼)並接受一些答案 – Asahi 2010-10-22 11:32:19