android.view.WindowLeaked异常 [英] android.view.WindowLeaked exception

查看:181
本文介绍了android.view.WindowLeaked异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个URL读取XML数据。它运作良好时,它是不是肖像模式。但我想将其更改为横向模式。但是,它被android.view.WindowLeaked例外。

请帮助我。提前致谢。这是我的code。

 包com.eisuru.abc;

进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.pm.ActivityInfo;
进口android.view.Menu;
进口android.widget.TextView;

公共类MainActivity延伸活动{

    TextView的tvResponse;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        tvResponse =(TextView中)findViewById(R.id.tvResponse);
        新PostAsync()执行()。
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }


    类PostAsync扩展的AsyncTask<虚空,虚空,虚空> {
        ProgressDialog PD; XMLHelper帮手;
        @覆盖
        在preExecute保护无效(){
            PD = ProgressDialog.show(MainActivity.this,汇率,加载汇率值......,真,假);
            }

        @覆盖
        保护无效doInBackground(虚空......为arg0){
            辅助=新XMLHelper(); helper.get();
            返回null;
            }

        @覆盖
        保护无效onPostExecute(虚空结果)
        {
            StringBuilder的建设者=新的StringBuilder();
            对于(Exrate_values​​职位:helper.exrates){

                builder.append(\ñ\ t+ post.getDate());
                builder.append(\ t \ t \ t+ post.getFrom_currency());
                builder.append(\ t \ t \ t+ post.getTo_Currency());
                builder.append(\ t \ t \ t+ post.getExrt_buy());
                builder.append(\ t \ t \ t \ t+ post.getExrt_sell());


                builder.append(\ N);

        }
                tvResponse.setText(builder.toString());
                pd.dismiss();
                }
        }

}
 

解决方案

在上一个活动对话框设置为可见的,但在方向改变的活动本身被破坏,那么它会导致泄露窗口错误。

有两种方法来处理这​​种情况: -

方法1
因此,需要撤销对话框活动的的onStop 的onDestroy 方法。例如:

  @覆盖
保护无效的onStop(){
    super.onStop();

    如果(PD!= NULL)
        pd.dismiss();
}
 

和定义活动类对话框

  ProgressDialog PD;
 

这个链接会帮助你<一个href="http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/">Handling进程对话和方向的变化

方法2
你必须把它添加到活动的声明在清单:

 安卓configChanges =方向
 

所以它看起来像

 &LT;活动机器人:标签=@字符串/ APP_NAME
        机器人:configChanges =方向| keyboardHidden
        机器人:名称=com.eisuru.abc.MainActivity&GT;
 

这个问题是系统破坏活动时,在配置发生变化。请参阅<一href="http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges">ConfigurationChanges.

所以把该配置文件中避免了系统摧毁你的活动。相反,它会调用 onConfigurationChanged(配置)方法。

I'm reading xml data from a url. It worked well when it was it portrait mode. But I wanted to change it to landscape mode. But it gets android.view.WindowLeaked exception.

Please help me with this. Thanks in advance. This is my code.

package com.eisuru.abc;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tvResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        tvResponse = (TextView) findViewById(R.id.tvResponse); 
        new PostAsync().execute(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    class PostAsync extends AsyncTask<Void, Void, Void> { 
        ProgressDialog pd; XMLHelper helper;     
        @Override 
        protected void onPreExecute() { 
            pd = ProgressDialog.show(MainActivity.this, "Exchange Rates", "Loading Exchange rates values ...", true, false);
            } 

        @Override 
        protected Void doInBackground(Void... arg0) { 
            helper = new XMLHelper(); helper.get(); 
            return null; 
            }   

        @Override 
        protected void onPostExecute(Void result) 
        { 
            StringBuilder builder = new StringBuilder(); 
            for(Exrate_values post : helper.exrates) {

                builder.append("\n\t " + post.getDate()); 
                builder.append("\t \t\t " + post.getFrom_currency()); 
                builder.append("\t \t\t " + post.getTo_Currency()); 
                builder.append("\t \t\t " + post.getExrt_buy()); 
                builder.append("\t \t\t\t " + post.getExrt_sell()); 


                builder.append("\n"); 

        } 
                tvResponse.setText(builder.toString()); 
                pd.dismiss(); 
                }   
        } 

}

解决方案

When a dialog on an activity is set to visible but on orientation changes the activity itself is destroyed, then it causes leaked window error.

There are two methods to handle this situation:-

Method 1
Therefore,you need to dismiss dialog in activity's onStop or onDestroy method. For example:

@Override
protected void onStop() {
    super.onStop();

    if(pd!= null)
        pd.dismiss();
}

and define dialog in activity class

ProgressDialog pd;

This link will help you Handling progress dialogs and orientation changes

Method 2
You have to add this to the activity declaration in the manifest:

android:configChanges="orientation"

so it looks like

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name="com.eisuru.abc.MainActivity">

The matter is that the system destroys the activity when a change in the configuration occurs. See ConfigurationChanges.

So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.

这篇关于android.view.WindowLeaked异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆