在 onCreate() 中调用对话框时出现黑屏

发布于 2024-08-04 19:44:50 字数 923 浏览 15 评论 0原文

我现在在几个不同的应用程序中遇到了这个问题,但似乎找不到解决方案。

如果在 Activity 的 onCreate() 中,我启动一个使用对话框主题的活动,它不会在屏幕上绘制任何内容...整个屏幕保持黑色。所有视图都在那里(例如,我可以点击 EditText 应该在的位置,它会给我键盘),它们只是不可见。

愚蠢的简单示例,为了好玩:

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非常简单......只需扩展Activity并将主题设置为@android:style/Theme.Dialog< /code> 在清单文件中。

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. All the views are there (e.g., I can tap where an EditText should be and it'll give me the keyboard), they're just not visible.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-08-11 19:44:50

事实证明,这是 1.5 中的已知错误 (在 1.6 中已修复,在 1.1 中从来没有出现过问题)。该错误源于新活动的动画在绘制旧活动之前发生,但仅当“旧”活动是任务中的第一个活动时才会出现。

解决方法是禁用主题的动画。最简单的方法是使用扩展主对话框主题的新主题。

res/values/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>

然后只需在 AndroidManifest.xml 中引用它:

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

显然,您失去了动画,但至少您可以看到它:)

注意:commonsware.com 的解决方案也可以很好地工作,并且符合我在评论。

It turns out that this is a known bug in 1.5 (fixed in 1.6 and never a problem in 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:

<?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:

<!-- ... -->
<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.

短暂陪伴 2024-08-11 19:44:50

只是猜测...

我认为 @android:style/Theme.Dialog 的大部分背景设置为半透明。最初,您的 MainActivity 的背景是黑色的。如果 startActivityForResult() 在您的 MainActivity 开始绘制之前启动,这可能可以解释您的问题。

尝试在 View 上使用 postDelayed()startActivityForResult() 延迟几百毫秒,然后看看是否会改变行为。

Just a guess here...

I think @android:style/Theme.Dialog is set for much of the background to be translucent. Initially, your MainActivity's background is black. If the startActivityForResult() is kicking in before your MainActivity gets to draw, that might explain your problem.

Try using postDelayed() on a View to delay your startActivityForResult() by a few hundred milliseconds, and see if that changes the behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文