我有一个异步任务,我在其中警告它应该是静态的,否则可能会发生泄漏。
所以我使用了这样的WeakReference:
private static class GetContacts extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
private WeakReference<Novinky> activityReference;
GetContacts(Novinky context) {
activityReference = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
// get a reference to the activity if it is still there
Novinky activity = activityReference.get();
if (activity == null || activity.isFinishing()) return;
super.onPreExecute();
dialog = new ProgressDialog(activity);
dialog.setMessage("Načítavam");
dialog.setTitle("Pripájam sa k serveru");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... args) {
HttpHandler sh = new HttpHandler();
String url = "https://www...";
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i < actors.length(); i++) {
JSONObject c = actors.getJSONObject(i);
Actors actor = new Actors();
actor.setLetter(c.getString("letter"));
actor.setNazov(c.getString("name"));
actor.setPerex(c.getString("perex"));
actorsList.add(actor);
}
} catch (final JSONException e) {
Novinky.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Novinky.this.getApplicationContext(),
"Chyba dát: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); }
return true;
} else {
Novinky.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Novinky.this.getApplicationContext(),
"Chyba internetového pripojenia.",
Toast.LENGTH_LONG).show();
}
});
return false;
}
}
protected void onPostExecute(Boolean result) {
// get a reference to the activity if it is still there
Novinky activity = activityReference.get();
if (activity == null || activity.isFinishing()) return;
super.onPostExecute(result);
dialog.dismiss();
adapter.notifyDataSetChanged();
}
}
现在警告消失了,但是由于更改,我仍然遇到其他错误。
1 /
actorsList.add(actor);
-在我的for循环中现在说不能从静态上下文中引用非静态字段'actorsList'2 /在放置runOnUiThread的catch和else语句中,我遇到
Novinky.this.runOnUiThread
问题-无法从静态上下文引用如果我仅用WeakReference(activityReference)替换Novinky.this,那么它说应该是类名,因此不确定在这些线程中如何正确替换Novinky.this。
我也尝试过使用
Novinky activity = activityReference.get();
,然后再使用activity.runOnUiThread
-这消除了错误,但是Novinky activity = activityReference.get();
的定义随后发出了警告该字段泄漏了上下文对象3 /最后一个问题在我的onPostExecute-
adapter.notifyDataSetChanged();
中。错误消息:无法从静态上下文引用非静态字段“适配器”更新:我以某种方式解决了它,现在我没有任何错误,并且该应用程序正在运行,但是仍然不确定我是否正确解决了该问题:
对于1 /,我在主类中定义了
static ArrayList<Actors> actorsList;
。2 /在捕获中,否则我定义了
final Novinky activity = activityReference.get();
接着:
activity.runOnUiThread
3 /在onPostExecute中,我使用了
activity.adapter.notifyDataSetChanged();
最佳答案
您应该能够以与访问适配器相同的方式访问列表的实例:
activityReference.get().actorsList