我在我的Activity的AsyncTask中初始化ProgressDialog
时遇到问题,java文件Activity的名称是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);
}
答案 0 :(得分:1)
进度对话框需要传递活动的上下文。正如你想的那样。
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();
完成干杯!