android activity 的弱引用
android WeakReference of activity
我有一个 Async Task,我收到一个警告,它应该是静态的,否则可能会发生泄漏。
所以我使用了这样的弱引用:
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 然后它说 class name is expected, 所以不确定如何在这些线程中正确替换 Novinky.this。
我也试过使用 Novinky activity = activityReference.get();
然后使用 activity.runOnUiThread
- 这消除了错误,但是 Novinky activity = activityReference.get();
的定义然后警告 This field leaks a上下文对象
3/ 最后一期在我的onPostExecute - adapter.notifyDataSetChanged();
。错误说:无法从静态上下文中引用非静态字段'adapter'
更新:我以某种方式解决了它,现在我没有错误,应用程序是 运行,但仍然不确定我是否正确解决了它:
对于1/我在主class.
中定义了static ArrayList<Actors> actorsList;
2/ 在 catch 中,否则我定义了
final Novinky activity = activityReference.get();
然后:
activity.runOnUiThread
3/ 在 onPostExecute 中我使用了 activity.adapter.notifyDataSetChanged();
您应该能够像访问适配器一样访问您的列表实例:
activityReference.get().actorsList
我有一个 Async Task,我收到一个警告,它应该是静态的,否则可能会发生泄漏。
所以我使用了这样的弱引用:
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 然后它说 class name is expected, 所以不确定如何在这些线程中正确替换 Novinky.this。
我也试过使用 Novinky activity = activityReference.get();
然后使用 activity.runOnUiThread
- 这消除了错误,但是 Novinky activity = activityReference.get();
的定义然后警告 This field leaks a上下文对象
3/ 最后一期在我的onPostExecute - adapter.notifyDataSetChanged();
。错误说:无法从静态上下文中引用非静态字段'adapter'
更新:我以某种方式解决了它,现在我没有错误,应用程序是 运行,但仍然不确定我是否正确解决了它:
对于1/我在主class.
中定义了static ArrayList<Actors> actorsList;
2/ 在 catch 中,否则我定义了
final Novinky activity = activityReference.get();
然后:
activity.runOnUiThread
3/ 在 onPostExecute 中我使用了 activity.adapter.notifyDataSetChanged();
您应该能够像访问适配器一样访问您的列表实例:
activityReference.get().actorsList