2010-10-22 79 views
-2

我的android應用程序顯示keydispatching超時錯誤。每當我觸摸屏幕時,我都不會發生。我發生應用程序啓動幾秒鐘後發生。這將是什麼原因?我使用了4個imageviews,oncreate()。當我觸摸圖像視圖時,它必須通過GPRS從服務器下載圖像。我在onCreate()方法中實現了主線程的完整操作。這是我的探頭嗎?我應該實現onStart(),onResume(),onPause(),onRestart()和onDestroy()方法嗎?android顯示錯誤

+1

你可以做至少兩件事情得到一些結果:後詳細info(日誌,代碼)並接受一些答案 – Asahi 2010-10-22 11:32:19

回答

2

你不能做加工是需要時間的任何顯着的程度(比如下載的東西從網絡中)在UI線程中,這是所有這些方法都被調用的地方。

您必須將這部分功能移到服務中或在循環線程中執行。

+2

請不要爲此使用服務。使用AsyncTask http://developer.android.com/intl/de/reference/android/os/AsyncTask.html – Janusz 2010-10-22 14:34:30

+0

我會在這方面向你推薦。我的主要觀點是讓它脫離UI線程。 – 2010-10-22 22:57:18

-1

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){// 運營跟進行動 }

3

我相信你會得到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