창을 추가 할 수 없습니다. android.os.BinderProxy 토큰이 유효하지 않습니다. 활동이 실행 중입니까?
Facebook API를 통해 Facebook에 연결하려고합니다. 다음 예를 따릅니다. https://github.com/facebook/facebook-android-sdk/tree/master/examples/simple
모든 것이 정상이지만 일부 코드를 편집하려고 할 때 로그인이 성공한 후 다음과 같이 대화 상자 게시 메시지를 표시하고 싶습니다.
public void onAuthSucceed() {
mText.setText("You have logged in! ");
//This is the code to call the post message dialog.
mFacebook.dialog(Example.this, "feed",new SampleDialogListener());
}
logcat에서이 오류를 수신합니다.
03-02 13:32:08.629: E/AndroidRuntime(14991): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405180f8 is not valid; is your activity running?
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.view.ViewRoot.setView(ViewRoot.java:532)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.app.Dialog.show(Dialog.java:241)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.Facebook.dialog(Facebook.java:780)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.Facebook.dialog(Facebook.java:737)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.Example$SampleAuthListener.onAuthSucceed(Example.java:113)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.SessionEvents.onLoginSuccess(SessionEvents.java:78)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.Example$LoginDialogListener.onComplete(Example.java:88)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.Facebook$1.onComplete(Facebook.java:320)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:144)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:218)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:337)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.os.Looper.loop(Looper.java:130)
03-02 13:32:08.629: E/AndroidRuntime(14991): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-02 13:32:08.629: E/AndroidRuntime(14991): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 13:32:08.629: E/AndroidRuntime(14991): at java.lang.reflect.Method.invoke(Method.java:507)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-02 13:32:08.629: E/AndroidRuntime(14991): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-02 13:32:08.629: E/AndroidRuntime(14991): at dalvik.system.NativeStart.main(Native Method)
어떤 생각?
더 이상 존재하지 않는 컨텍스트에 대한 대화 상자를 표시 할 때 발생할 수 있습니다. 일반적인 경우- '대화 상자 표시'작업이 비동기 작업 이후이고 해당 작업 중에 원래 활동 (대화 상자의 부모)이 삭제되는 경우입니다. 좋은 설명은 다음 블로그 게시물과 댓글을 참조하십시오.
http://dimitar.me/android-displaying-dialogs-from-background-threads/
위의 스택 추적에서 facebook 라이브러리가 인증 작업을 비동기 적으로 스핀 오프하는 것으로 보이며이 시나리오를 쉽게 만들 수있는 Handler-Callback 메커니즘 (리스너에서 onComplete 호출)이 있습니다.
내 앱에서보고 된 것을 보았을 때 매우 드물고 블로그 게시물의 경험과 일치합니다. 활동에 문제가 발생했거나 AsyncTask 작업 중에 파괴되었습니다. 매번 수정 사항이 어떻게 발생할 수 있는지 모르겠지만 코드가 실행될 때 항상 파괴되는 대화 상자의 컨텍스트로 Activity를 참조하고 있습니까?
또한 이것이 활동이 실행 중인지 확인하는 가장 좋은 방법인지 확실하지 않지만 수행하는 한 가지 방법에 대해서는 다음 답변을 참조하십시오.
내 앱 중 일부에서이 오류가 가끔보고되는 것을 보았는데,이 문제가 해결 된 이유는 다음과 같습니다.
if(!((Activity) context).isFinishing())
{
//show dialog
}
다른 모든 답변은 달리기 활동 목록을 반복하는 것과 같은 이상한 일을하는 것 같지만 이것은 훨씬 간단하고 트릭을 수행하는 것 같습니다.
한 가지 간단한 해결 방법은 예외를 포착하는 것입니다.
try {
alertDialog.show()
}
catch (WindowManager.BadTokenException e) {
//use a log message
}
우아하지는 않지만 비동기 작업을 관리해야하고 대화 상자를 표시 할 때 활동이 실행 중인지 여부를 확신 할 수없는 경우에는 때때로 쉽습니다.
제 경우에는
onPostExecute
AsyncTask 에서 대화 상자를 열거 나 표시하려고하기 때문에 문제가 발생했습니다.그러나 잘못된 방법
showing dialog
또는 Ui가onPostExecute
.For that, we need to check the activity is in active Eg :
!isFinishing()
, if the activity is not finished then only able to show our dialog box or ui change.@Override protected void onPostExecute(String response_str) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { if (!((Activity) mContext).isFinishing()) { try { ShowAgilDialog(); } catch (WindowManager.BadTokenException e) { Log.e("WindowManagerBad ", e.toString()); } } } }); }
I faced exactly the same issue. Calling '(!isFinishing())'
prevented the crash, but it could not display the 'alert' message.
Then I tried making calling function 'static', where alert is displayed. After that, no crash happened and message is also getting displayed.
For ex:
public static void connect_failure() {
Log.i(FW_UPD_APP, "Connect failed");
new AlertDialog.Builder(MyActivity)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
})
.setIcon(R.drawable.the_image).show();
}
After execute the thread, add these two line of code, and that will solve the issue.
Looper.loop();
Looper.myLooper().quit();
For me it was fixed by just removing the static
property in the DialogHelper.class methods (resposble for creating & hiding the dialog), as I have properties related to Window
in those methods
Under dependencyServices nothing of the above helped me, i ended up doing like below:
class Static_Toast_Android
{
private static Context _context
{
get { return Android.App.Application.Context; }
}
public static void StaticDisplayToast(string message)
{
Toast.MakeText(_context, message, ToastLength.Long).Show();
}
}
public class Toast_Android : IToast
{
public void DisplayToast(string message)
{
Static_Toast_Android.StaticDisplayToast(message);
}
}
I must use the "double-class" because an interface cannot be static. L-
'Programing' 카테고리의 다른 글
어떤 $ _SERVER 변수가 안전합니까? (0) | 2020.08.25 |
---|---|
C #에서 정수 배열을 합산하는 방법 (0) | 2020.08.24 |
부트 스트랩을 사용한 반응 형 iframe (0) | 2020.08.24 |
Kotlin의 Swift if let 문 (0) | 2020.08.24 |
이미지를 바이트 배열로 변환하는 방법 (0) | 2020.08.24 |