我需要在asynctask运行时向用户显示一个微调器:
public class RecoverListTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog spinner;
private Context context;
public RecoverListTask(Context c) {
context = c;
spinner = new ProgressDialog(context); // spinner
}
@Override
protected void onPreExecute() {
// show progress spinner
spinner.setMessage("Downloading Podcats List");
spinner.show();
//do something
}
@Override
protected void onPostExecute(Void v) {
spinner.dismiss();
}
}
(我只粘贴了关于微调器的部分)但是当我尝试运行app时,我有这个错误:
11-05 18:02:49.077: E/WindowManager(28986): Activity com.example.podcast610downloader.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4264ef28 V.E..... R....... 0,0-684,192} that was originally added here
11-05 18:02:49.077: E/WindowManager(28986): android.view.WindowLeaked: Activity com.example.podcast610downloader.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4264ef28 V.E..... R....... 0,0-684,192} that was originally added here
11-05 18:02:49.077: E/WindowManager(28986): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
11-05 18:02:49.077: E/WindowManager(28986): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
11-05 18:02:49.077: E/WindowManager(28986): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.Dialog.show(Dialog.java:281)
11-05 18:02:49.077: E/WindowManager(28986): at com.example.podcast610downloader.RecoverListTask.onPreExecute(RecoverListTask.java:31)
11-05 18:02:49.077: E/WindowManager(28986): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-05 18:02:49.077: E/WindowManager(28986): at android.os.AsyncTask.execute(AsyncTask.java:534)
11-05 18:02:49.077: E/WindowManager(28986): at com.example.podcast610downloader.MainActivity.onCreate(MainActivity.java:24)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.Activity.performCreate(Activity.java:5133)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-05 18:02:49.077: E/WindowManager(28986): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 18:02:49.077: E/WindowManager(28986): at android.os.Looper.loop(Looper.java:137)
11-05 18:02:49.077: E/WindowManager(28986): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-05 18:02:49.077: E/WindowManager(28986): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 18:02:49.077: E/WindowManager(28986): at java.lang.reflect.Method.invoke(Method.java:525)
11-05 18:02:49.077: E/WindowManager(28986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-05 18:02:49.077: E/WindowManager(28986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 18:02:49.077: E/WindowManager(28986): at dalvik.system.NativeStart.main(Native Method)
onCreate code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//recover list of available podcast (with details)
RecoverListTask task = new RecoverListTask(this);
task.execute(null, null, null);
listView = (ListView) findViewById(R.id.listView);
arrayAdapter=new ArrayAdapter<PodcastFile>(this, android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
问题是什么?
答案 0 :(得分:0)
不要保留对上下文变量的引用。它显然是无用的,这就是保留你的活动实例的东西。
只需删除以下行:
private Context context;
context = c;
EDIT_的 _ __ _ __ _ ___ 强>
好的,我会这样做:
public class MainActivity extends Activity
{
private ProgressDialog spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = new ProgressDialog(this);
// show progress spinner
spinner.setMessage("Downloading Podcats List");
spinner.show();
// recover list of available podcast (with details)
RecoverListTask task = new RecoverListTask();
task.execute(null, null, null);
}
private void onLoadingComplete()
{
spinner.dismiss();
}
public class RecoverListTask extends AsyncTask<Void, Void, Void>
{
public RecoverListTask()
{}
@Override
protected void onPreExecute()
{
}
@Override
protected void onPostExecute(Void v)
{
onLoadingComplete();
}
@Override
protected Void doInBackground(Void... params)
{
// do something
//If you want to get a reference to the Activity use
//MainActivity.this instead of context
return null;
}
}
}
答案 1 :(得分:0)
我认为在实例化Asynctask
时,您正在通过getApplicationContext()
new RecoverListTask(getApplicationContext()).execute();
相反,尝试这样做
new RecoverListTask(MainActivity.this).execute();
检查它是否有效!