Android 개발 노트: Dialog 사용 상세 정보
05-15 02:45:26.320: E/AndroidRuntime(1161): java.lang.IllegalArgumentException: View not attached to window manager
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.view.Window$LocalWindowManager.removeView(Window.java:432)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.app.Dialog.dismissDialog(Dialog.java:278)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.app.Dialog.access$000(Dialog.java:71)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.app.Dialog$1.run(Dialog.java:111)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.app.Dialog.dismiss(Dialog.java:268)
05-15 02:45:26.320: E/AndroidRuntime(1161): at com.hilton.effectiveandroid.app.DialogDemo$1.handleMessage(DialogDemo.java:26)
05-15 02:45:26.320: E/AndroidRuntime(1161): at android.os.Handler.dispatchMessage(Handler.java:99)
7. Dialog.show()는 주 스레드에서 호출되어야 하지만 Dialog.dismiss () 는 모든 라인에서 호출할 수 있습니다.세 가지 사용 방식 비교 1.부분적인 Dialog 대상을 직접 만드는 장점은 변수가 부분적인 이해와 유지보수가 쉽다는 것이다.단점은 Dialog 객체를 제어하기 어려워 Runtime Exception을 유발하기 쉽다는 것입니다.2. Dialog 객체를 Activity로 변환하는 도메인의 장점은 Dialog 객체를 재사용할 수 있고 Activity는 Dialog가 Activity 라이프 사이클 외부에 표시되지 않도록 제어할 수 있다는 것입니다.추천하는 사용법입니다.3. 액티비티의 방법으로 onCreate Dialog(), show Dialog(), dismiss Dialog()의 장점은 Frameworks가 Dialog를 돌볼 수 있도록 도와준다는 것이다. 대부분의 경우 추천하는 방법이다.그러나 액티비티가 조기 죽는 경우 이 방법은 반드시 Runtime Exception이 있고 피할 수 없다.인스턴스
public class DialogDemo extends Activity {
private static final int DISMISS_DIALOG = 1;
private ProgressDialog mBetterDialog;
private Handler mMainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DISMISS_DIALOG:
Dialog dialog = (Dialog) msg.obj;
dialog.dismiss();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_demo);
final Button sucking = (Button) findViewById(R.id.sucking);
sucking.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Activity activity = DialogDemo.this;
final ProgressDialog dialog = new ProgressDialog(activity);
dialog.setTitle("Worst dialogging");
dialog.setMessage("This is the worst dialogging scheme, NEVER use it. This dialog is easy to " +
"run out of its attached activity, yielding WindowManager#BadTokenException if the activity is gone when dismissing");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
// You MUST do the show in main thread anyway
dialog.show();
new Thread(new Runnable() {
public void run() {
SystemClock.sleep(10000);
/*
* IllegalArgumentException: View not attached to window manager
* If the activity showing the dialog was killed before dismiss() out of rotation or locale changed,
* the dialog will gone with activity, but when dismiss() yields "IllegalArgumentException: View not attached to
* window manager".
* Checking isShowing() won't help.
* Checking activity.isFinishing() won't help, either.
* Dismiss it in main thread also won't give any help.
*/
// THIS WON't WORK
// if (dialog.isShowing()) {
// dialog.dismiss();
// }
// if (!activity.isFinishing()) {
// dialog.dismiss();
// }
Message msg = Message.obtain();
msg.what = DISMISS_DIALOG;
msg.obj = dialog;
mMainHandler.sendMessage(msg);
}
}).start();
}
});
final Button better = (Button) findViewById(R.id.better);
better.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mBetterDialog = new ProgressDialog(DialogDemo.this);
mBetterDialog.setTitle("Better dialogging");
mBetterDialog.setMessage("This dialogging can be used. The dialog object is a field of its activity, so activity can" +
" control it to make sure dialog only lives within activity lifecircle");
mBetterDialog.setIndeterminate(true);
mBetterDialog.setCancelable(true);
// You MUST do the show in main thread anyway
mBetterDialog.show();
new Thread(new Runnable() {
public void run() {
SystemClock.sleep(10000);
/*
* This is much better, mBetterDialog is a field of its activity, so activity can take care of it in order
* to make sure dialog only live within activity's life circle, to avoid any unexpected exceptions.
*/
// THIS really works
if (mBetterDialog != null && mBetterDialog.isShowing()) {
mBetterDialog.dismiss();
}
}
}).start();
}
});
final Button optional = (Button) findViewById(R.id.optional);
optional.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
showDialog(0);
new Thread(new Runnable() {
public void run() {
SystemClock.sleep(10000);
/*
* This way works best for most of time, except if activity died before dismissing, exception must be
* thrown: "IllegalArgumentException: View not attached to window manager".
* Although activity takes care of its belonging dialog, there is no way to operate it manually any more.
* First you do not have reference to dialog object and second, any manual operation only interferences
* and breaks state maintained by frameworks.
*/
dismissDialog(0);
}
}).start();
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
ProgressDialog d = new ProgressDialog(this);
d.setTitle("Optional dialogging");
d.setMessage("This dialogging scheme works best for most times, the dialogs are all taken care of by activitys and frameworks" +
". Except for activity being killed during dialog showing");
d.setIndeterminate(true);
d.setCancelable(true);
return d;
}
@Override
protected void onDestroy() {
super.onDestroy();
// Activity is dying, all its belonging dialogs should be dismissed, of course.
if (mBetterDialog != null && mBetterDialog.isShowing()) {
mBetterDialog.dismiss();
mBetterDialog = null;
}
// For dialogs showed via showDialog(int), no way to stop it in onDestroy()
// dismissDialog(0); // cause "IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog"
// This is because Activity has to manage its dialog during onPause() and onResume() to restore
// dialogs' state. So if you manually dismiss it in onDestroy(), it will cause JE.
// removeDialog(0);// cause "IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog", when
// dismissing in thread.
// This is because Activity has to manage its dialog during onPause() and onResume() to restore
// dialogs' state. So if you manually dismiss it in onDestroy(), it will cause JE.
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.