我正在从url中读取xml数据。当它是肖像模式时,它工作得很好。但我想把它变成景观模式。但是它得到了android.view.WindowLeaked异常。
请帮我处理这个。提前谢谢。这是我的密码。
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();
}
}
}
发布于 2014-07-02 21:25:04
当活动上的对话框被设置为可见但在方向上改变时,活动本身就被破坏了,那么它会导致泄漏窗口错误。
处理这种情况的方法有两种:
方法1
因此,您需要在activity的onStop
或onDestroy
方法中使用onDestroy
对话框。例如:
@Override
protected void onStop() {
super.onStop();
if(pd!= null)
pd.dismiss();
}
并在活动类中定义对话框。
ProgressDialog pd;
此链接将帮助您Handling progress dialogs and orientation changes。
方法2
您必须将其添加到清单中的活动声明中:
android:configChanges="orientation"
所以看起来
<activity android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:name="com.eisuru.abc.MainActivity">
问题是,当配置发生更改时,系统会破坏该活动。见ConfigurationChanges。
因此,将其放在配置文件中可以避免系统破坏您的活动。相反,它调用onConfigurationChanged(Configuration)
方法。
发布于 2014-07-02 21:46:16
退出某个活动后,您正在尝试显示对话框。
解决方案是在退出活动(例如,在Example.java:183 ()中)之前,在您在onPause中创建的对话框上调用before ()。在离开一个活动之前,所有的窗口和对话框都应该关闭。
或
将此添加到您的清单中:
android:configChanges="orientation|keyboardHidden
然后在您的活动中添加以下内容:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
//ur Code
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
//ur Code
}
}
发布于 2015-07-13 05:31:25
对话框是属于主线程的子线程,如果要显示或杀死它们,必须在OnUiThread上这样做。我使用片段,当我显示对话框时,得到这个异常。但这个方法救了我。
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.show();//dismiss any dialog like this
}
});
https://stackoverflow.com/questions/24545471
复制相似问题