在我的活动的ProgressDialog中初始化AsyncTask有困难,java文件活动的名称是BigBoard.java。
下面是AsyncTask的java类代码,它位于BigBoard.java内部:
class syncX extends AsyncTask<String, String, String>
{
ProgressDialog progress;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progress = new ProgressDialog(context); //ERROR
progress.setMessage("Setting BigBoard ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}
@Override
protected String doInBackground(String... params)
{
return null;
}
@Override
protected void onPostExecute(String file_url)
{
}
}现在,这就是我遇到麻烦的地方,progress = new ProgressDialog(context);。我试着把它改成:
progress = new ProgressDialog(this);
progress = new ProgressDialog(BigBoard);
progress = new ProgressDialog(BigBoard.this);但是,无助。怎么修呢?
EDIT1
BigBoard类按请求在下面提供。
public class BigBoard extends ActionBarActivity {
ArrayList<String> countryLocal;
String temp;
ArrayAdapter<String> namesArrayAdapter;
ContactInfo ci;
List<ContactInfo> result;
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Parse.initialize(this, "app-id", "client-key");
setContentView(R.layout.activity_big_board);
ci = new ContactInfo();
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String abc="hello";
syncX runner = new syncX();
runner.execute();
result = new ArrayList<ContactInfo>();
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> credentialList, ParseException e) {
if (e == null) {
for(int i=0;i<credentialList.size();i++)
{
ci.name = credentialList.get(i).getString("Name");
ci.surname = credentialList.get(i).getString("SurName");
ci.email = credentialList.get(i).getString("email");
result.add(ci);
Log.d("OUT", "So the Val::------> " + credentialList.get(i).getString("email"));
//result.notifyDataSetChanged();
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
ContactAdapter ca = new ContactAdapter(result);
recList.setAdapter(ca);
}发布于 2015-10-25 03:55:03
进度对话框需要传递活动的上下文。就像你想要的那样。
progress = new ProgressDialog(context);现在的问题是如何将上下文传递给进度对话框。只是这门课的召唤。class syncX extends AsyncTask<String, String, String>{}应该将上下文传递给进度对话框。请参阅下面的修改版本类。
class syncX extends AsyncTask<String, String, String>
{
ProgressDialog progress;
Context mContext;
public syncX(Context context){
this.mContext= context;
}
public Context getContext(){
return mContext;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
progress = new ProgressDialog(getContext());
progress.setMessage("Setting BigBoard ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}
@Override
protected String doInBackground(String... params)
{
return null;
}
@Override
protected void onPostExecute(String file_url)
{
}
}在您的BigBoard类中,应该是这样的。
new syncX(this).execute();干杯!
https://stackoverflow.com/questions/33325957
复制相似问题