我的應用程序在後臺線程中執行一些網絡活動或從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)
雅,它也適用於我,原因是當我們縱向或LandScape設備顯示ProgressBar時,那麼方法onDestroy()將被調用。 – String 2013-04-04 12:29:04