gpt4 book ai didi

android - 进度对话框和后台线程处于 Activity 状态时处理屏幕方向更改?

转载 作者:行者123 更新时间:2023-11-29 22:10:05 26 4
gpt4 key购买 nike

我的应用程序在后台线程中执行一些网络 Activity 或从 Web 服务获取数据。在开始之前,它会弹出一个进度对话框。对话框在处理程序上被关闭。这一切都很好,除非在对话框启动时屏幕方向发生变化此时我的应用程序崩溃了。我在 res/layout 文件夹中使用两种布局,一种纵向,一种在 res/中布局土地。当我将屏幕方向从纵向更改为横向或反之亦然时,我的应用程序会崩溃。

我知道为什么会这样。

当我切换方向时,Android 将创建一个新 View 。我可能会崩溃,因为\后台线程正在尝试更改旧线程的状态。 (它也可能有问题,因为后台线程不在 UI 线程上)。所以任何 friend 都可以告诉我如何解决这个问题,以便我可以继续。

当进度对话框和后台线程处于 Activity 状态时,如何处理屏幕方向变化?

提前致谢。

我的代码在这里

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();

}

我的日志猫
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)

最佳答案

我找到了我的问题的解决方案并这样做了

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 {

}
}
}

}

让我知道它对你有用,我们不是

关于android - 进度对话框和后台线程处于 Activity 状态时处理屏幕方向更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9828456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com