本文介绍了Android的 - 传递查看从活动到的AsyncTask类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MainActivity.java:
private class NetCheck extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... args) {
return cd.isConnectingToInternet();
}
protected void onPostExecute(Boolean th) {
if (th == true) {
new RemoteLoader(MainActivity.this).execute();
}
}
}
RemoteLoader.java:
public class RemoteLoader extends AsyncTask<Void, Void, Void>{
private View view;
public RemoteLoader(View context){
this.view = view;
}
@Override
protected Void doInBackground(Void... Pages) {
// do in bg
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) view.findViewById(R.id.txtProtip);
txttitle.setText(protip);
}
}
我试图执行从 MainActivity
RemoteLoader
类。但在RemoteLoader包含一些需要膨胀主要布局。
I'm trying to execute RemoteLoader
class from MainActivity
. But in RemoteLoader contains something that needs to inflate the Main layout.
我如何通过从布局到MainActivity在RemoteLoader这种情况?
How do I pass the Layout from MainActivity to RemoteLoader in this case?
推荐答案
如下传递活动的RemoteLoader,而不是视图
pass an Activity to the RemoteLoader instead of View as follows
private WeakReference<Activity> activity;
public RemoteLoader(Activity context){
this.activity = new WeakReference<Activity>(context);
}
更新:使用弱引用prevent可能的内存泄漏
Update: Using weak reference to prevent possible memory leaks.
这篇关于Android的 - 传递查看从活动到的AsyncTask类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!