0

我有一个像这样扩展活动的简单类

public class About extends Activity
{
  private ProgressDialog pdia;
  private TextView tvAbout;
  private WebView vwAbout;
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);


    vwAbout = (WebView)findViewById(R.id.vwViewAbout);
    String content = new String();
    try {
        URL url = new URL("http://xxx.prodevAbout.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null)
        {
            content+=str + "\n";
            vwAbout.loadData(content, "text/html", null);
        }
        in.close();
    } catch (Exception e)
    {
        vwAbout.loadData("error message", "text/html", null);
    }
  }
}

我想要做的是我想在从网站加载内容时添加一个进度条,我找到了这段代码

 private ProgressDialog pdia;

 @Override
 protected void onPreExecute(){ 
    super.onPreExecute();
         pdia = new ProgressDialog(yourContext);
         pdia.setMessage("Loading...");
         pdia.show();    
 }

 @Override
 protected void onPostExecute(String result){
    super.onPostExecute(result);
         pdia.dismiss();
 }

并添加了此代码,但无法解决 onPreExecute 方法我该怎么办?

谢谢;

4

2 回答 2

0

添加了此代码,但无法解决 onPreExecute 方法我该怎么办?

onPreExecute未解决,因为这是方法,AsyncTask因此需要扩展AsyncTask类以使用 onPreExecute 使用 AsyncTask 更改当前代码:

public class About extends Activity
{
   [...your code here...]
  public void onCreate(Bundle savedInstanceState)
  {

   [...your code here...]
   // execute AsyncTask as 
    new LongOperation(About.this).execute("");
  }
  [...your code here...]

   public class WebapiOperation extends AsyncTask<String, Void, String> {
     Context context;
     WebapiOperation(Context context){
     this.context=context;
    }
      @Override
      protected void onPreExecute() {
         // show ProgressDialog here
         pdia = new ProgressDialog(context);
      }
      @Override
      protected String doInBackground(String... params) {

             // make web service access task here
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {  
            // ProgressDialog here
      }

}
于 2013-01-06T14:24:25.013 回答
0

我在我的应用程序中添加进度对话框的方式(到目前为止我从未遇到任何问题)是这样的:

Runnable runable = new Runnable() {

        @Override
        public void run() {

            SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
                        progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this, ProgressDialog.THEME_HOLO_LIGHT);
                    } else {
                        progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this);
                    }
                    progressDialog.setMessage("Loading! Pleasе wait...");
                    progressDialog.setIndeterminate(true);
                    progressDialog.show();
                }
            });


            // CONNECT TO SERVER AND DOWNLOAD DATA


            SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {

                    progressDialog.dismiss();

                }
            });
            }
        }
    };
    new Thread(runable).start();
于 2013-01-06T14:25:11.537 回答