Android asynctask 不接受上下文
Android asynctask won't accept context
我有一个奇怪的问题。我正在尝试更改 TextView 但不能 findViewById
,它说无法解析 findViewById
。我有一个 Main Activity,我正在从中调用 AsyncTask(Context context)
。在构造函数中,我从 main activity 传递了它,我已经尝试使用 Main Activity.getContext() 但它仍然不起作用。
欢迎任何帮助。
public class UcitavanjeUpozadini extends AsyncTask<Void, Void, Void> {
private Context context;
private ProgressDialog progressDialog;
private UcitavanjeHelperKlasa helperKlasa;
private List<OglasHelper> lista;
public UcitavanjeUpozadini(Context context){
this.context = context;
this.progressDialog = null;
this.helperKlasa = new UcitavanjeHelperKlasa();
this.lista = new ArrayList<OglasHelper>();
}
这是在片段中还是在 activity 中?您是否尝试过 getApplicationContext()?
findViewById 不是 Context 的函数,它是 Activity 的函数。通常,您应该直接传入视图而不是 Activity 并在其上调用 findViewById。
如果你有 activity,你应该这样做,在 onPostExecute 中更改 TextView 值:
private MyActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTextView = (TextView) findViewById(R.id.textview);
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{
@Override
protected void onPreExecute(){
mTextView.setText("text changed before background operation");
}
@Override
protected Boolean doInBackground(Void... params){
//perform your background operations changing the paramaters
//and return type accordingly
return true;
}
@Override
protected void onPostExecute(Boolean result){
mTextView.setText("text changed after background operation");
}
}
}
如果您需要将 AsyncTask 放在单独的文件中,您可以向 Activity 中实现的接口添加回调,或者在实例化期间将 TextView 传递给 AsyncTask
我有一个奇怪的问题。我正在尝试更改 TextView 但不能 findViewById
,它说无法解析 findViewById
。我有一个 Main Activity,我正在从中调用 AsyncTask(Context context)
。在构造函数中,我从 main activity 传递了它,我已经尝试使用 Main Activity.getContext() 但它仍然不起作用。
欢迎任何帮助。
public class UcitavanjeUpozadini extends AsyncTask<Void, Void, Void> {
private Context context;
private ProgressDialog progressDialog;
private UcitavanjeHelperKlasa helperKlasa;
private List<OglasHelper> lista;
public UcitavanjeUpozadini(Context context){
this.context = context;
this.progressDialog = null;
this.helperKlasa = new UcitavanjeHelperKlasa();
this.lista = new ArrayList<OglasHelper>();
}
这是在片段中还是在 activity 中?您是否尝试过 getApplicationContext()?
findViewById 不是 Context 的函数,它是 Activity 的函数。通常,您应该直接传入视图而不是 Activity 并在其上调用 findViewById。
如果你有 activity,你应该这样做,在 onPostExecute 中更改 TextView 值:
private MyActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTextView = (TextView) findViewById(R.id.textview);
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{
@Override
protected void onPreExecute(){
mTextView.setText("text changed before background operation");
}
@Override
protected Boolean doInBackground(Void... params){
//perform your background operations changing the paramaters
//and return type accordingly
return true;
}
@Override
protected void onPostExecute(Boolean result){
mTextView.setText("text changed after background operation");
}
}
}
如果您需要将 AsyncTask 放在单独的文件中,您可以向 Activity 中实现的接口添加回调,或者在实例化期间将 TextView 传递给 AsyncTask