2016-09-10 23 views
-2

爲Android創建測驗應用程序,我使用Firebase存儲我的數據。當應用程序啓動時,所有問題都在onCreate()方法中加載,但問題在於我的應用程序在加載完成之前啓動並且崩潰。我想要做的是加載屏幕,並且問題在加載完成之前不會啓動。任何幫助都將不勝感激。爲Android測驗應用程序的Firebase載入數據

謝謝。

+0

您忘記附上[重現問題的最小代碼](http://stackoverflow.com/help/mcve)。沒有看到,這將很難幫助。 –

+0

代碼中沒有問題,我需要的是在開始時製作加載屏幕,當所有問題都從Firebase加載完畢時結束加載屏幕,但我對如何做到這一點沒有任何想法。 – Massadi

回答

0

當您從Firebase加載大量數據時,最好在單獨的線程中獲取它,並在完成獲取後更新主要活動。 爲此,您可以使用ASYNC任務。

class fetchdata extends AsyncTask 
{ 
@Override 
    protected void onPreExecute() { 
     // Runs on the UI thread before doInBackground() 
    } 

@Override 
    protected Object doInBackground(Object... params) { 
     // Fetch data here 
     Log.d(TAG, "[email protected] from another thread"); 

     return new Object(); 
    } 
@Override 
    protected void onPostExecute(Result result) { 
     // Runs on the UI thread after doInBackground() 
//update main activity here 
    } 
} 

創建異步任務等級後,你必須把它調用主類:

fetchdata obj = new fetchdata(); 
obj.execute();//this will execute async task seperatly. 

你也可以使用進度對話框的時間數據在異步任務獲取。

相關問題