我有一个badtokenexception的报告,尽管我尝试了任何东西,但我无法重现它,但我不清楚它是如何发生的。
BadTokenException(@ android.view.ViewRootImpl:setView:575)via(@ android.view.WindowManagerGlobal:addView:272)FULL TRACE:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os。 BinderProxy @ e5346b6无效;你的活动在运行吗?在Android.view上的android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)的android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)上的android.view.ViewRootImpl.setView(ViewRootImpl.java:575)。 Dialog.show(Dialog.java:306)位于android.support.v上的android.support.v4.app.DialogFragment.onStart(SourceFile:416),android.support.v4上的android.support.v4.app.Fragment.performStart(SourceFile:2369) .app.FragmentManagerImpl.moveToState(的SourceFile:1458)在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(的SourceFile:1740)在android.support.v4.app.FragmentManagerImpl.moveToState(的SourceFile:1809)在android.support。 v4.app.FragmentManagerImpl.dispatchStateChange(的SourceFile:3217)在android.support.v4.app.FragmentManagerImpl.dispatchStart(的SourceFile:3176)在android.support.v4.app.FragmentController.dispatchStart(的SourceFile:203)在android.support .v4.app.FragmentActivity.onStart(SourceFile:562)在android.a的android.support.v7.app.AppCompatActivity.onStart(SourceFile:177) pp.Instrumentation.callActivityOnStart(Instrumentation.java:1220)在android.app.Activity.performStart(Activity.java:5993)在android.app.Activity.performRestart(Activity.java:6050)在android.app.ActivityThread.handleWindowVisibility (ActivityThread.java:3453)在android.app.ActivityThread.access $ 1200(ActivityThread.java:148)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1317)在android.os.Handler.dispatchMessage(处理程序.java:102)在android.app.Looper.loop(Looper.java:135)的android.app.ActivityThread.main(ActivityThread.java:5272)at java.lang.reflect.Method.invoke(Native Method)at at java.lang.reflect.Method.invoke(Method.java:372)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:909)在com.android.internal.os.ZygoteInit.main( ZygoteInit.java:704)
我有一个活动,在启动时会显示一个对话框,通常它可以正常工作,只会在分析中报告几个崩溃。
public class DialogFragmentWrapperActivity extends AppCompatActivity {
/** Stores an instance of the dialog to be shown. */
private CustomDialogFragment mCustomDialog = null;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fragmentId = "DialogFragmentWrapperActivity" + String.valueOf(this.hashCode());
if ((savedInstanceState != null) && savedInstanceState.containsKey("id")) {
fragmentId = savedInstanceState.getString("id");
}
mCustomDialog = CustomDialogFragment.newInstance(fragmentId);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(mCustomDialog, fragmentId);
ft.commit();
}
您是否有一些暗示如何发生恶意感冒?我唯一可以想到的是,如果在调用ft.commit()之前活动被关闭,那么我之前尝试调用finish()但这并没有改变任何东西。一些想法?
答案 0 :(得分:0)
您可以使用isFinishing()方法检查活动是否正在结束。
public class DialogFragmentWrapperActivity extends AppCompatActivity {
/** Stores an instance of the dialog to be shown. */
private CustomDialogFragment mCustomDialog = null;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fragmentId = "DialogFragmentWrapperActivity" + String.valueOf(this.hashCode());
if ((savedInstanceState != null) && savedInstanceState.containsKey("id")) {
fragmentId = savedInstanceState.getString("id");
}
mCustomDialog = CustomDialogFragment.newInstance(fragmentId);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(mCustomDialog, fragmentId);
if(!isFinishing()){
ft.commit();
}
}