简体   繁体   English

在onCreate()中调用Dialogs时显示黑屏

[英]Solid black screens when calling Dialogs in onCreate()

I've had this problem in a few different apps now and I can't seem to find a solution. 我现在在几个不同的应用程序中遇到了这个问题,我似乎无法找到解决方案。

If, in the onCreate() of an Activity , I start an activity that uses the dialog theme it doesn't draw anything to screen... the whole screen stays black. 如果,在一个Activity的onCreate()中,我启动一个使用对话框主题的活动,它不会绘制任何东西到屏幕...整个屏幕保持黑色。 All the views are there (eg, I can tap where an EditText should be and it'll give me the keyboard), they're just not visible. 所有的视图都在那里(例如,我可以点击EditText应该在哪里,它会给我键盘),它们只是不可见。

Stupid simple example, for fun: 愚蠢的简单例子,为了好玩:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);
        startActivityForResult(new Intent(this, CredentialsInputActivity.class), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // do some crap with the result, doesn't really matter what
    }
}

CredentialsInputActivity is pretty straight forward... just extends Activity and has the theme set to @android:style/Theme.Dialog in the manifest file. CredentialsInputActivity非常简单......只需扩展Activity并将主题设置为清单文件中的@android:style/Theme.Dialog

It turns out that this is a known bug in 1.5 (fixed in 1.6 and never a problem in 1.1). 事实证明,这是1.5中的一个已知错误 (在1.6中修复,在1.1中从来没有问题)。 The bug stems from the animation for the new Activity taking place before the old Activity has been drawn, but it only presents if the "old" Activity was the first Activity in the Task. 该错误源于在绘制旧活动之前发生的新活动的动画,但它仅在“旧”活动是任务中的第一个活动时才会出现。

A workaround is to disable the animation for the theme. 解决方法是禁用主题的动画。 The simplest way to do this it with a new theme that extends the main dialog theme. 最简单的方法是使用扩展主对话框主题的新主题来实现它。

res/values/themes.xml: RES /值/的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CupcakeDialog" parent="android:Theme.Dialog">
        <item name="android:windowAnimationStyle">@null</item>
    </style>
</resources>

Then just reference it in your AndroidManifest.xml: 然后在AndroidManifest.xml中引用它:

<!-- ... -->
<activity 
    android:name=".CredentialsInputActivity"
    android:label="@string/CredentialsInputActivity_window_title"
    android:theme="@style/CupcakeDialog" />
<!-- ... -->

Obviously, you loose the animation, but at least you can see it :) 显然,你放松了动画,但至少你可以看到:)

Note: commonsware.com's solution worked fine too with the caveat I noted in the comments. 注意:commonsware.com的解决方案也很好用我在评论中提到的警告。

Just a guess here... 在这里猜一下......

I think @android:style/Theme.Dialog is set for much of the background to be translucent. 我认为@android:style/Theme.Dialog设置为大部分背景都是半透明的。 Initially, your MainActivity 's background is black. 最初,您的MainActivity的背景是​​黑色的。 If the startActivityForResult() is kicking in before your MainActivity gets to draw, that might explain your problem. 如果startActivityForResult()在您的MainActivity得到绘制之前就已经开始了,这可能会解释您的问题。

Try using postDelayed() on a View to delay your startActivityForResult() by a few hundred milliseconds, and see if that changes the behavior. 尝试在View上使用postDelayed()startActivityForResult()延迟几百毫秒,看看是否会改变行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在onCreate中调用findViewById时发生NullPointerException - NullPointerException when calling findViewById in onCreate 调用invalidate后,Android Custom View显示纯黑色 - Android Custom View shows solid black after calling invalidate Android OpenGLES2.0-渲染时为纯黑色纹理 - Android OpenGLES2.0 - Solid Black Textures When Rendered 调用服务时,运行onCreate,但不运行onStartCommand() - When calling service, onCreate is run but not onStartCommand() 横屏时,Android在finish()之后调用onCreate() - Android calling onCreate() after finish() when in landscape 自定义对话框上的黑带 - Black bands on custom dialogs 应用程序子类中的onCreate方法在应用程序打开时不会每次都调用 - onCreate method in subclass of Application not calling every time when app open 为什么片段在使用FrameLayout的片段事务时调用OnCreate / OnCreateView? - Why is fragment calling OnCreate / OnCreateView when using fragment transactions with FrameLayout? 从onCreate调用addPreferencesFromResource时在PreferenceFragment中使用onCreateView时出现奇怪错误 - Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate Android - 如何在activity的onCreate()中调用setTitle()时避免延迟 - Android - How to avoid delay when calling setTitle() in activity's onCreate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM