我的应用程序执行一些网络活动或从后台线程中的webservices获取数据。在开始之前,它会弹出一个进度对话框。该对话框在处理程序上被解除。这一切都正常,除非在对话框启动时屏幕方向发生变化此时我的应用程序崩溃了。我在res / layout文件夹中使用两个布局作为一个肖像,在res /中使用一个格局布局 - 土地。当我将屏幕方向从纵向更改为横向或反之亦然时,我的应用会崩溃。
我知道为什么会这样。
当我切换方向时,Android会创建一个新视图。我可能会崩溃,因为\后台线程正试图改变旧的状态。 (它可能也有问题,因为后台线程不在UI线程上)。所以任何好友都可以告诉我如何解决这个问题,以便我可以继续。
如果进度对话框和后台线程处于活动状态,如何处理屏幕方向更改?
提前致谢。
我的代码在这里
public class LoginActivity extends Activity {
EditText edUserName, edPassword;
Button btnLogin;
TextView txt_Alert;
ProgressDialog dialog;
String url, Status, result;
ConnectToWebService cn;
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
dialog.dismiss();
if (check_Status()) {
login();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("Login");
edUserName = (EditText) findViewById(R.id.edName);
edPassword = (EditText) findViewById(R.id.edPass);
btnLogin = (Button) findViewById(R.id.btnLogin);
txt_Alert = (TextView) findViewById(R.id.txt_Required);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String uName = edUserName.getText().toString().trim();
String Pass = edPassword.getText().toString();
cn = new ConnectToWebService();
processThread();
}
protected void processThread() {
dialog = ProgressDialog.show(LoginActivity.this, "",
"Singing in ...", false);
new Thread() {
public void run() {
Status = cn.callWebService(url);
handler.sendEmptyMessage(0);
}
}.start();
}
我的LogCat
03-23 00:03:48.865: E/AndroidRuntime(11799): java.lang.IllegalArgumentException: View not attached to window manager
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.Window$LocalWindowManager.removeView(Window.java:432)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.dismissDialog(Dialog.java:278)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.access$000(Dialog.java:71)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog$1.run(Dialog.java:111)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.dismiss(Dialog.java:268)
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.palewar.coinop.LoginActivity$1.handleMessage(LoginActivity.java:42)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.os.Looper.loop(Looper.java:130)
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-23 00:03:48.865: E/AndroidRuntime(11799): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 00:03:48.865: E/AndroidRuntime(11799): at java.lang.reflect.Method.invoke(Method.java:507)
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
03-23 00:03:48.865: E/AndroidRuntime(11799): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我为我的问题找到解决方案并按照这样做完
package com.palewar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class ThreadActivity extends Activity {
static ProgressDialog dialog;
private Thread downloadThread;
final static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
dialog.dismiss();
}
};
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
super.onDestroy();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadThread = (Thread) getLastNonConfigurationInstance();
if (downloadThread != null && downloadThread.isAlive()) {
dialog = ProgressDialog.show(ThreadActivity.this, "",
"Signing in...", false);
}
dialog = ProgressDialog.show(ThreadActivity.this, "",
"Signing in ...", false);
downloadThread = new MyThread();
downloadThread.start();
// processThread();
}
// Save the thread
@Override
public Object onRetainNonConfigurationInstance() {
return downloadThread;
}
static public class MyThread extends Thread {
@Override
public void run() {
try {
// Simulate a slow network
try {
new Thread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
} finally {
}
}
}
}
让我知道它对我们都有用
答案 1 :(得分:0)
而不是使用ProgressDialog直接覆盖Activity的OnCreateDialog函数。或者我相信你可以设置对话框的父级是哪个活动。
http://developer.android.com/reference/android/app/Activity.html#onCreateDialog(int,android.os.Bundle)
这应保留对话框,允许您通过活动的dismissDialog调用
将其关闭答案 2 :(得分:0)
HIII ,,
为了获得最佳优化,您必须使用:
1.如果您的情况不需要,将Activty的方向固定为某些肖像或风景。
2.使用onOrientationChange()并仔细处理事件。当您要显示进度对话框时,如果方向更改窗口管理器进入操作并且不存在执行操作的视图(例如显示对话框事件) )
3.简单的解决方案,但不是很好
wihtin try
{
processThread();
/**basically this line dialog = ProgressDialog.show(LoginActivity.this, "",
"Singing in to CoinOp...", false);*/
}
catch (Exception e);