【问题标题】:progress dialog getting error进度对话框出现错误
【发布时间】:2012-08-07 09:53:42
【问题描述】:

我已经检查了这个链接并实现了以下,但我仍然收到错误 ProgressDialog : how to prevent Leaked Window

这是我的代码

public class MyClass extends AsyncTask<Void , Void , Void>
{       

    private ProgressDialog progress;
    public MyClass(ProgressDialog progress)
    {
        this.progress = progress;
    }
    public void onPreExecute()
    {

        progress.show();
    }
    protected Void doInBackground(Void... params) {
        try {               
            /**
             * Create a new instance of the SAX parser
             **/
            SAXParserFactory saxPF = SAXParserFactory.newInstance();
            SAXParser saxP = saxPF.newSAXParser();
            XMLReader xmlR = saxP.getXMLReader();
            URL url = new URL("http://zaggle.foostor.in/xml/get_product_by_id/"+((MyApplication) getApplicationContext()).categoryid); // URL of the XML        
            /** 
             * Create the Handler to handle each of the XML tags. 
             **/
            XMLHandlerforDetailsofproduct myXMLHandler = new XMLHandlerforDetailsofproduct();
            xmlR.setContentHandler(myXMLHandler);
            xmlR.parse(new InputSource(url.openStream()));              
        } catch (Exception e) {
            System.out.println(e);
        }
        data = XMLHandlerforDetailsofproduct.data;
        for (int i = 0; i < data.getdenominationsvalue().size(); i++) {

            denomvalue.add(data.getdenominationsvalue().get(i));
            denomid.add(data.getdenominationsid().get(i));
            disc.add(data.getdiscount().get(i));
            stock.add(data.getstock().get(i));

        }           



        return null;            
    }

    public void onPostExecute(Void unused)
    {

        this.progress.dismiss();
        this.progress = null;

    }       

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.store_denomination);
    spin1 = (Spinner)findViewById(R.id.spin);   
    ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("Loading......");
    new MyClass(progress).execute();
            ---------------------------------

and the code continues
}

这是我的日志:

08-10 10:36:19.500: E/WindowManager(835): Activity com.androidhive.xmlparsing.Detailsofproduct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f15250 that was originally added here
08-10 10:36:19.500: E/WindowManager(835): android.view.WindowLeaked: Activity com.androidhive.xmlparsing.Detailsofproduct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f15250 that was originally added here
08-10 10:36:19.500: E/WindowManager(835):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-10 10:36:19.500: E/WindowManager(835):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-10 10:36:19.500: E/WindowManager(835):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-10 10:36:19.500: E/WindowManager(835):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.Dialog.show(Dialog.java:241)
08-10 10:36:19.500: E/WindowManager(835):   at com.androidhive.xmlparsing.Detailsofproduct$MyClass.onPreExecute(Detailsofproduct.java:75)
08-10 10:36:19.500: E/WindowManager(835):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-10 10:36:19.500: E/WindowManager(835):   at com.androidhive.xmlparsing.Detailsofproduct.onCreate(Detailsofproduct.java:127)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 10:36:19.500: E/WindowManager(835):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 10:36:19.500: E/WindowManager(835):   at android.os.Looper.loop(Looper.java:123)
08-10 10:36:19.500: E/WindowManager(835):   at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 10:36:19.500: E/WindowManager(835):   at java.lang.reflect.Method.invokeNative(Native Method)
08-10 10:36:19.500: E/WindowManager(835):   at java.lang.reflect.Method.invoke(Method.java:521)
08-10 10:36:19.500: E/WindowManager(835):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 10:36:19.500: E/WindowManager(835):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 10:36:19.500: E/WindowManager(835):   at dalvik.system.NativeStart.main(Native Method)

谁能指出哪里做错了?

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    我认为这可能是您的解决方案

    首先删除你的构造函数和在 preExcecute 方法中创建的进度对话框实例,像这样

    protected void onPreExecute() {
                    dialog=new ProgressDialog(Activity.this);
                    dialog.show();
                    super.onPreExecute();
                }
    

    【讨论】:

    • @user1411729 如果回答解决了您的问题,请接受... :)
    【解决方案2】:

    嗯……

    根据我的经验,ProgressDialog 是在 Myclass 中创建的。

    如果你在 onreate 中创建 ProgressDialog 并且 Myclass(extends AnsyncTask) 从自己的构造函数中获取 ProgressDialog,它可能会抛出错误。

    public class MyClass extends AsyncTask {
    private ProgressDialog progress = new ProgressDialog(getparent());
    
    /* public MyClass(ProgressDialog progress)
    {
        this.progress = progress;
    } */
    
    public void onPreExecute()
    {
    
        progress.show();
    }
    protected Void doInBackground(Void... params) {
        try {               
            /**
             * Create a new instance of the SAX parser
             **/
    

    ....

    这样使用。

    我的代码已成功执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多