我有一个简单的类,像这样扩展了活动
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方法无法解决我该怎么办?
感谢;
答案 0 :(得分: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
}
}
答案 1 :(得分: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();