当我尝试运行代码时,只有主要的Toast正在运行。 Progress Dialog和其他toast消息没有运行。这个程序是睡眠过程的简单异步示例。主要问题是它没有显示Progressdialog。
我是否需要添加xml文件(它只包含一个textView和一个Button)。 请帮我解决这个问题。谢谢你
package com.example.asyncexample;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
public class MainActivity extends Activity {
ProgressDialog progressBar;
int prorgessInc = 1; // incrementing the progress dialog
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.start_button);
button.setOnClickListener(startTaskListener);
}
private OnClickListener startTaskListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
progressBar = new ProgressDialog(v.getContext());
BackgroundTask test = new BackgroundTask();
test.execute(context);
CharSequence text = "Main Thread is Running";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
};
private class BackgroundTask extends AsyncTask<Context, Integer, String>{
protected void OnPreExecute() {
CharSequence msg = "BackgroundTask is Operating";
progressBar.setCancelable(true);
progressBar.setMessage(msg);
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
}
@Override
protected String doInBackground(Context... params) {
//BackgroundTask Is Running
for(int i =0; i<=100; i+=prorgessInc){
try {Thread.sleep(100);}
catch (InterruptedException e) { e.printStackTrace();}
publishProgress(prorgessInc);
if(isCancelled()) break;
}
return getString(R.string.backgndcompld);
}
protected void OnProgressUpdate(Integer...values ) {
//Update Progress bar
progressBar.incrementProgressBy(prorgessInc);
}
protected void PostExecute(String result){
//Dissmiss progressbar
progressBar.dismiss();
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast2 = Toast.makeText(context, result, duration);
toast2.show();
}
}
}
答案 0 :(得分:1)
是的,您的问题出在AsyncTask的方法命名中。
您已定义:
然而真正的方法是:
注意案例的区别。如果您使用了@Override
,那么您的IDE可能会向您展示。
我已经对代码的其他部分进行了一些清理。看看:
public class MainActivity
extends Activity
implements View.OnClickListener
{
private Button button;
private ProgressDialog progressBar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_example);
button = (Button) findViewById(R.id.start_button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
Log.d("click", "start button clicked, starting task");
BackgroundTask test = new BackgroundTask();
test.execute();
}
private class BackgroundTask
extends AsyncTask<Void, Integer, String>
{
int PROGRESS_INCREMENT = 1;
int PROGRESS_MAX = 100;
String DIALOG_MSG = "AsyncTask is Operating";
@Override
protected void onPreExecute()
{
progressBar = new ProgressDialog(getApplicationContext());
progressBar.setCancelable(true);
progressBar.setMessage(DIALOG_MSG);
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(PROGRESS_MAX);
progressBar.show();
}
@Override
protected String doInBackground(Void... params)
{
for (int i = 0; i <= 100; i += PROGRESS_INCREMENT)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
publishProgress(PROGRESS_INCREMENT);
if (this.isCancelled())
{
break;
}
}
return "[some result text]";
}
@Override
protected void onProgressUpdate(Integer... values)
{
progressBar.incrementProgressBy(PROGRESS_INCREMENT);
}
@Override
protected void onPostExecute(String result)
{
progressBar.dismiss();
Toast.makeText(getApplicationContext(),
result,
Toast.LENGTH_LONG)
.show();
}
}
}
值得注意的变化: