为什么在此代码中获取windowManager $ BadTokenException?

时间:2012-10-10 07:57:33

标签: android

public class ReviewActivity extends Activity {
     public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reviewfrag_layout);
         mInflater = (LayoutInflater) ReviewActivity.this.getSystemService( 
                    LAYOUT_INFLATER_SERVICE); 
            ContextThemeWrapper mTheme = new ContextThemeWrapper(this, 
                    R.style.customDialog); 

            mView = mInflater.inflate(R.layout.custom_dialog, null); 
            // mDialog = new Dialog(this,0); // context, theme 

            mDialog = new Dialog(mTheme); 
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
            mDialog.setContentView(this.mView); 

               //if i comment below line  code runs otherwise give exception
           mDialog.show();
        }
}

风格:

<style name="customDialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowContentOverlay">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>

记录消息: 10-10 13:20:01.184:E / AndroidRuntime(1417):致命异常:主要 10-10 13:20:01.184:E / AndroidRuntime(1417):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager $ LocalActivityRecord@411eeaa0无效;你的活动在运行吗? 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.ViewRootImpl.setView(ViewRootImpl.java:585) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:149) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.Window $ LocalWindowManager.addView(Window.java:547) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.app.Dialog.show(Dialog.java:277) 10-10 13:20:01.184:E / AndroidRuntime(1417):at com.mds.restau.activity.ReviewActivity $ 1.onClick(ReviewActivity.java:74) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.View.performClick(View.java:4084) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.view.View $ PerformClick.run(View.java:16966) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.os.Handler.handleCallback(Handler.java:615) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.os.Handler.dispatchMessage(Handler.java:92) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.os.Looper.loop(Looper.java:137) 10-10 13:20:01.184:E / AndroidRuntime(1417):在android.app.ActivityThread.main(ActivityThread.java:4745) 10-10 13:20:01.184:E / AndroidRuntime(1417):at java.lang.reflect.Method.invokeNative(Native Method) 10-10 13:20:01.184:E / AndroidRuntime(1417):at java.lang.reflect.Method.invoke(Method.java:511) 10-10 13:20:01.184:E / AndroidRuntime(1417):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 10-10 13:20:01.184:E / AndroidRuntime(1417):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-10 13:20:01.184:E / AndroidRuntime(1417):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

这是背景问题。如果您从Activity组中打开此活动,则使用以下命令而不是使用setContentView(R.layout.xxxx):

View viewToLoad = LayoutInflater.from(getParent())。inflate(R.layout.XXXX,null);         this.setContentView(viewToLoad);

希望它能起作用