如何在片段中添加AsyncTask类

时间:2013-11-13 06:49:30

标签: android android-asynctask android-fragmentactivity

嗨,我是新手如何在片段中添加asynctask类,我在new ProgressTask().execute();中有错误我想在屏幕上添加进度条..

这是我的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_zudioz, container, false);
    setHasOptionsMenu(true);


    webView = (WebView)rootView.findViewById(R.id.webView1);
    bar = (ProgressBar) rootView.findViewById(R.id.progressBar);
    new ProgressTask().execute();
    private class ProgressTask extends AsyncTask <Void,Void,Void>{
        @Override
        protected void onPreExecute(){
            bar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... arg0) {   
               //my stuff is here
        }

        @Override
        protected void onPostExecute(Void result) {
              bar.setVisibility(View.GONE);
        }
    }

    webView.loadUrl("http://www.zudioz.com");
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient());           
    webView.getSettings().setAllowFileAccess(true); 
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setAppCacheMaxSize(1024*1024*8);                              
    return rootView;
}

3 个答案:

答案 0 :(得分:1)

试试如下:

class ProgressTask extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
                    progressDialog = new ProgressDialog(getActivity());
            progressDialog .setMessage("Loading...");
            progressDialog .setCancelable(false);
            progressDialog .show();
        }

        @Override
        protected String doInBackground(String... params) {
            return string;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();
        }
    }

答案 1 :(得分:0)

而不是进度条,你可以这样写:

ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();

在您的异步任务的onPreExecute()

答案 2 :(得分:0)

你犯过的错误是你在onCreateView()内创建了ProgressTask,而是在onCreateView()之外创建(让我们说类级别)并使用onCreateView()方法在execute()内执行它AsyncTask。

例如:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_zudioz, container, false);
    setHasOptionsMenu(true);


    webView = (WebView)rootView.findViewById(R.id.webView1);
    bar = (ProgressBar) rootView.findViewById(R.id.progressBar);
    new ProgressTask().execute();
    ....
    ....
}

 private class ProgressTask extends AsyncTask <Void,Void,Void>{
        @Override
        protected void onPreExecute(){
            bar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void doInBackground(Void... arg0) {   
               //my stuff is here
        }

        @Override
        protected void onPostExecute(Void result) {
              bar.setVisibility(View.GONE);
        }
    }