4

在我的应用程序中,我正在从 Web 服务器获取数据。我使用异步任务来获取数据。但是,问题有时是我的应用程序在 onPostExecute on 崩溃progressDialog.dismiss()。但是,这种情况并非每次都会发生。这是我的代码:

异步任务

public class GetFeaturedStoreListAsyncTask extends AsyncTask<Void, Void, String>
{
Context ctx;
ProgressDialog pd;
ListView listFeaturedStore;
String dataUrl;
public static final String TAG = "GetFeaturedStoreAsyncTask";
public static ArrayList<StoreDescriptionDAO> featuredDAOList;

public GetFeaturedStoreListAsyncTask(String dataUrl, Context ctx, ListView listFeaturedStore) 
{
    this.dataUrl = dataUrl;
    this.ctx = ctx;
    this.listFeaturedStore = listFeaturedStore;
}//Constructor

@Override
protected void onPreExecute() 
{
    pd = new ProgressDialog(ctx);
    pd.setMessage(ctx.getResources().getString(R.string.please_wait));
    pd.show();
}//onPreExecute

@Override
protected String doInBackground(Void... params) 
{   
    Log.v(TAG, "doInBackground called");
    JSONParser jsonParser = new JSONParser();
    String serverResponse = jsonParser.getJsonString(dataUrl);
    return serverResponse;
}//doInBackground

@Override
protected void onPostExecute(String response) 
{
    FeaturedListAdapter mFeaturedListAdapter = null;
    mFeaturedListAdapter = getFeaturedListAdapter(response);

    if(mFeaturedListAdapter != null)
    {
        Log.v(TAG, "adapter not null");
        listFeaturedStore.setAdapter(mFeaturedListAdapter);
        listFeaturedStore.setLayoutAnimation(AnimationUtility.fastCascadeListViewAnimation());
        pd.dismiss();
    }//if
    else
    {
        Log.v(TAG, "adapter null");
        pd.dismiss();
        SeattleNightLifeUtility.OpenUtilityDialog(ctx, 
                ctx.getResources().getString(R.string.network_error_msg));
    }//else
}//onPostExecute

private FeaturedListAdapter getFeaturedListAdapter(String jString) 
{
    FeaturedListAdapter mFeaturedAdapter = null;
    featuredDAOList = 
            ParsedFeaturedStoreDescData.getFeaturedStoreDesc(jString);
    if(featuredDAOList != null 
            && featuredDAOList.size() > 0)
    {
        mFeaturedAdapter = new FeaturedListAdapter(ctx, featuredDAOList);
        Log.v(TAG, "arraylist size > 0");
        return mFeaturedAdapter;
    }//if
    else
    {
        return null;
    }//else
}//getFeaturedListAdapter
}//GetCityStoreAsyncTask

这是我的日志:

Activity com.dzo.seattlesnightlife.FeaturedActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a715c38 that was originally added here
02-02 15:23:45.426: E/WindowManager(12627): android.view.WindowLeaked: Activity com.dzo.seattlesnightlife.FeaturedActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a715c38 that was originally added here
02-02 15:23:45.426: E/WindowManager(12627):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
02-02 15:23:45.426: E/WindowManager(12627):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-02 15:23:45.426: E/WindowManager(12627):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-02 15:23:45.426: E/WindowManager(12627):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-02 15:23:45.426: E/WindowManager(12627):     at android.app.Dialog.show(Dialog.java:241)
02-02 15:23:45.426: E/WindowManager(12627):     at com.dzo.seattlesnightlife.asynctask.GetFeaturedStoreListAsyncTask.onPreExecute(GetFeaturedStoreListAsyncTask.java:40)
02-02 15:23:45.426: E/WindowManager(12627):     at android.os.AsyncTask.execute(AsyncTask.java:391)
02-02 15:23:45.426: E/WindowManager(12627):     at com.dzo.seattlesnightlife.FeaturedActivity$MyTimerTask$1.run(FeaturedActivity.java:173)
02-02 15:23:45.426: E/WindowManager(12627):     at android.os.Handler.handleCallback(Handler.java:587)
02-02 15:23:45.426: E/WindowManager(12627):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 15:23:45.426: E/WindowManager(12627):     at android.os.Looper.loop(Looper.java:123)
02-02 15:23:45.426: E/WindowManager(12627):     at android.app.ActivityThread.main(ActivityThread.java:4633)
02-02 15:23:45.426: E/WindowManager(12627):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 15:23:45.426: E/WindowManager(12627):     at java.lang.reflect.Method.invoke(Method.java:521)
02-02 15:23:45.426: E/WindowManager(12627):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-02 15:23:45.426: E/WindowManager(12627):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-02 15:23:45.426: E/WindowManager(12627):     at dalvik.system.NativeStart.main(Native Method)
02-02 15:23:45.456: V/GetFeaturedStoreAsyncTask(12627): arraylist size > 0
02-02 15:23:45.456: V/GetFeaturedStoreAsyncTask(12627): adapter not null
02-02 15:23:45.526: W/dalvikvm(12627): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
02-02 15:23:45.536: E/AndroidRuntime(12627): FATAL EXCEPTION: main
02-02 15:23:45.536: E/AndroidRuntime(12627): java.lang.IllegalArgumentException: View not attached to window manager
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.app.Dialog.dismissDialog(Dialog.java:278)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at  android.app.Dialog.access$000(Dialog.java:71)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.app.Dialog$1.run(Dialog.java:111)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.app.Dialog.dismiss(Dialog.java:268)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at com.dzo.seattlesnightlife.asynctask.GetFeaturedStoreListAsyncTask.onPostExecute(GetFeaturedStoreListAsyncTask.java:63)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at com.dzo.seattlesnightlife.asynctask.GetFeaturedStoreListAsyncTask.onPostExecute(GetFeaturedStoreListAsyncTask.java:1)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.os.AsyncTask.finish(AsyncTask.java:417)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.os.Looper.loop(Looper.java:123)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at android.app.ActivityThread.main(ActivityThread.java:4633)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at java.lang.reflect.Method.invoke(Method.java:521)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-02 15:23:45.536: E/AndroidRuntime(12627):    at dalvik.system.NativeStart.main(Native Method)
02-02 15:23:45.556: W/ActivityManager(180):   Force finishing activity com.dzo.seattlesnightlife/.FeaturedActivity
4

3 回答 3

2

ProgressDialog在您退出后尝试显示 a 时会出现此问题Activity

我刚刚解决了这个问题试试这个方法:

if ((this.mDialog != null) && this.mDialog.isShowing()) {
    this.mDialog.dismiss();
    this.mDialog = null;
 }

在您的覆盖onDestroy()onStop()方法中。

编辑:您可以创建接口,将其传递给 AsyncTask(在构造函数中),然后在 onPostExecute 中调用方法。

例如

你的界面:

public interface OnTaskCompleted {
    void onTaskCompleted();
}

您的活动:

public YourActivity implements OnTaskCompleted {
    //your Activity
}

还有你的 AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object> { //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener) {
        this.listener=listener;
    }

//required methods

    protected void onPostExecute(Object o) {
        //your stuff
        listener.onTaskCompleted();
    }
}
于 2013-02-02T10:24:44.747 回答
0

当试图在ProgerssDialog这里显示我在您的代码中遇到的问题时会出现此问题,您必须在ProgressDialog创建时指定您当前的活动上下文。

onPostExecute

if (null != pd && pd.isShowing()) {
    pd.dismiss();
}
于 2013-02-02T10:34:55.100 回答
0

似乎问题出在您的适配器类中。如果您查看日志条目。看起来在打印适配器不为空之后出现异常。所以请确保您的适配器工作正常。我想告诉的另一件事是。为什么你是两次使用相同的代码。

pd.dismiss();

如果在这两种情况下,您都将其解散。最好通过在它当前所在的 if else 语句上方写下它来使其全局化。

于 2013-02-02T10:38:16.823 回答