0

在我的应用程序中,我正在从 URL 检索一些数据,因此,我在应用程序中实现了进度条,AsyncTask但应用程序既不显示进度条,也不显示我的数据。如果我loadDataFromURL()在方法中调用方法,onCreate()那么我的数据会加载,但是我必须等待一段时间。请在下面检查我的代码。

public class MainActivity extends Activity 
{
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();

    }

    private void loadDataFromURL() 
    {
        // Here I load some data from URL
    }




    ProgressDialog dialog;

    class LoadData extends AsyncTask<Void, Void, Void> 
    {
        protected void onPreExecute() 
        {
                   dialog = ProgressDialog.show(context, "Loading...", null);
        }

        protected Void doInBackground(Void... unused) 
    {
            try 
            {
                loadDataFromURL();

            Log.d("-DONE", "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
            } 
            catch(Exception e) 
            {
                Log.d("-ERORR", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
            } 

            return null;
        }

        protected void onProgressUpdate(Void... unused) 
        {
        }

        protected void onPostExecute(Void unused) 
        {
            dialog.dismiss();
        }
    }


}
4

3 回答 3

0

You are missing the call for the async task, add

new LoadData().execute();

in your onCreate

于 2013-05-26T11:42:48.847 回答
0

Can you execute or run AsyncTask from our Activity Oncreate.

run AsyncTask via LoadData.execute();

This Method run the AsyncTask .

Thanks

于 2013-05-26T11:43:55.960 回答
0

Asyntask 在 UI 线程上加载。您应该调用 execute 来加载异步任务。此外,您添加了 @Override 注释,因为它的覆盖方法。您可以参考以下内容。你的 doInbackground 什么都不做。所以你可能看不到对话框,因为 asynctask 完成它的操作非常快。进行一些计算,以便您可以看到对话框。

public class MainActivity extends Activity 
{

@Override 
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dialog = new ProgressDialog(MainActivity.this);
    dialog.setTitle("Loading...");
    new LoadData().execute();  // load asynctsk on ui thread

}

private void loadDataFromURL() 
{
    // Here I load some data from URL
}   

class LoadData extends AsyncTask<Void, Void, Void> 
{
    @Override // missing the override annotation
    protected void onPreExecute() 
    {
        super.onPreExecute() // missing super call
        dialog.show();    // show dialog   
    }
    @Override
    protected Void doInBackground(Void... unused) 
    {   
        try 
        {
            loadDataFromURL(); // do network related operation here

            Log.d("-DONE", "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
            //do not update ui here 
        } 
        catch(Exception e) 
        {
            Log.d("-ERORR", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
        } 

        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {

        super.onProgressUpdate(values); // missing super call
}
    @Override
    protected void onPostExecute(Void result) 
    {
         super.onPostExecute(result) // missing super call
        dialog.dismiss();
    }
}
}

请检查下面的链接以获取更多信息

http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-05-26T14:04:17.583 回答