0

嗨,我是 android 新手,如何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;
}
4

3 回答 3

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();
        }
    }
于 2013-11-13T06:59:32.220 回答
0

您犯的错误是您在内部创建了 ProgressTask onCreateView(),而不是在外部创建它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);
        }
    }
于 2013-11-13T07:03:04.933 回答
0

代替进度条,你可以这样写:

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

在你的异步任务的 onPreExecute()

于 2013-11-13T06:55:50.593 回答